C # Seven steps to connect to a SQL database

Source: Internet
Author: User

The C # connection to the SQL database also requires validation, and Windows authentication is a Windows-only authentication system used by SQL Server servers. When establishing a database connection, you also need to take into account the validity of the address.

C # Seven steps to connect a SQL database:

1, the establishment of SqlConnection objects;

2. Specify the connectionstring attribute of the SqlConnection object;

3, open the database connection;

4, specify the SQL statement;

5. Establish SqlDataAdapter object and DataSet object (MyDataAdapter = new SqlDataAdapter (Strcmd,conn);

);

6, Tim Chong DataSet;

7. Specify a data source for the DataGrid

  1. SqlConnection conn=new SqlConnection ();
  2. Conn. connectionstring="datasource= (local); Initialcatalog=test;userid=sa;password=sa";
  3. Conn. Open ();
  4. string strcmd;
  5. strcmd="SELECT * from Test";
  6. SqlDataAdapter MyDataAdapter;
  7. DataSet mydataset=new dataset ();
  8. mydataadapter=New SqlDataAdapter (Strcmd,conn);
  9. Mydataadapter.fill (myDataSet,"test");
  10. mytestdatagrid.datasource=mydataset.tables["test"];

Description: The database is installed locally (local); The database name is test; the user name is sa; the password is sa;

The table in the database is test;

A seven-step C # connection to SQL database is described here.

C # Seven steps to connect to a SQL database

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.