Let's introduce the following connection to the database:
We first connect the data via the Connection object:
string str1 = Application.startuppath; String str = "PROVIDER=MICROSOFT.JET.OLEDB.4.0; Data source= "[email protected]" \TELEPHONE.MDB; Persist Security info=false "; OleDbConnection conn = new OleDbConnection (str);
In the connection string: Provider= refers to the file that provides the service, data sourc= refers to the location of the database file, Persist Security info= refers to thewhether or not to save security information can be simply understood as "whether ADO saves password information after the database connection is successful."
Then there is the Datagriviewdatasource, where we can do it in two ways:
First, we can set the data binding implementation of Datagriview:
First we select Datagrivew Add in the toolbar, this is the top right we can find a button, click to:
Then click the Select Data Source button to add:
entered the data source selection interface: We can select the data set, then we can (choose the data file, and the location, no longer described here).
The second type: The binding of the database to the Datagriview through code:
There is a DataSource member in Datagriview we specify it as a connection to get the data source dataset:
For datasets, we need to establish a connection conn connection through the Conn.Open (), implement a connection database, get a dataset in the same DataAdapter DA, and then pass Da.fill (ds, "Phone") method to add the dataset to the dataset["phone".
The code is as follows:
private void Form1_Load (object sender, EventArgs e) {//TODO: This line of code loads the data into the table "Telephonedataset.telephone Info ". You can move or delete it as needed. This.telephoneinfoTableAdapter.Fill (this.telephoneDataSet.telephoneinfo);//Auto-add?? Enter form = new Enter (); Form. ShowDialog (); if (Enter.flag = = 0) {this. Close (); } DGV. DataSource = Bandsource (). tables["Phone"]; The public DataSet Bandsource () {string str1 = Application.startuppath; String str = "PROVIDER=MICROSOFT.JET.OLEDB.4.0; Data source= "[email protected]" \TELEPHONE.MDB; Persist Security Info=false "; OleDbConnection conn = new OleDbConnection (str); ds = new DataSet (); try {Conn. Open (); String sqlstr = "Select Telephoneinfo. PersonID as auto-numbered, Telephoneinfo. Name as names, Telephoneinfo. Sex as gender, telephoneinfo. Officetel as Office phone, Telephoneinfo. HOmetel as Home phone, telephoneinfo. Mark as notes from Telephoneinfo order by PersonID "; da = new OleDbDataAdapter (SQLSTR, conn); Da. Fill (ds, "Phone"); } catch (Exception e) {MessageBox.Show (e.message); } finally {Conn. Close (); } return DS; }
Simple application of C # database-datagriview connection database and more related operations