Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

C # connects to Access database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

C # connects to Access database

Microsoft Office Access is a relational database management system released by Microsoft. It combines the characteristics of MicrosoftJet Database Engine and graphical user interface, and is one of the system programs of Microsoft Office.

For many small software, using access database is a good choice, I will briefly introduce the use of C # to connect to the database.

Step 1: we set up a project in vs2015, named AccessTest.

Project establishment

Step 2: create a new Access database file, named test, and place it on the desktop.

Database file

Step 3: add a button button to the winform form.

Step 4: add a reference System.Data.OleDb

Using System.Data.OleDb

Step 5: double-click the button button to add code

/ Access database test connection button / private void button1_Click (object sender, EventArgs e) {try {/ / create the string string reportpath to connect to the database string reportpath = @ "C:\ Users\ 15543\ Desktop\ test.mdb"; / / if my mdb file is test.mdb, the location is desktop string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0" Data source= "+ reportpath+"; Uid=Admin;Pwd=jcvadmin; "; / create oledbconnection object OleDbConnection con = new OleDbConnection (ConStr); con.Open (); / / Open database MessageBox.Show (" OK "); con.Close (); / / close database MessageBox.Show (" NO "); / / the following comment is to select data from a table and load it into Listview / / string str =" select * from TestTable " / / load all data in the table / / OleDbCommand cmd = new OleDbCommand (str, con); / / OleDbDataReader oldbRed = cmd.ExecuteReader (); / / while (oldbRed.Read ()) / / if Read () is not called, there will be no data. / / {/ / ListViewItem lvi = new ListViewItem (oldbRed [0] .ToString ()); / / lvi.SubItems.Add (oldbRed [1] .ToString ()); / / lvi.SubItems.Add (oldbRed [2] .ToString ()); / / lvi.SubItems.Add (oldbRed [3] .ToString ()); / / lvi.SubItems.Add (oldbRed [4] .ToString ()) / / lvi.SubItems.Add (oldbRed [5] .ToString ()); / / listView1.Items.Add (lvi); / /} / / oldbRed.Close (); / / con.Close (); / / con.Dispose ();} catch (Exception err) {/ / if there is an error, the error message MessageBox.Show (err.Message.ToString ());}}

Path

Button codes and comments

The last step is debugging and running.

Open the database

Shut down the database

The connection is successful. Congratulations, the database test connection is successful. Then you can operate on the database through the relevant statements.

This tutorial is for Xiaobai, do not like do not spray the following will introduce some of the database operation statements and practical application. Welcome to continue reading. The source code address is attached below.

The database has to be set up and the path has to be modified.

Running environment VS2015

Free source address: http://down.51cto.com/data/2215791

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report