Simple application of C # database-datagriview connection database and more related operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.