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
- SqlConnection conn=new SqlConnection ();
- Conn. connectionstring="datasource= (local); Initialcatalog=test;userid=sa;password=sa";
- Conn. Open ();
- string strcmd;
- strcmd="SELECT * from Test";
- SqlDataAdapter MyDataAdapter;
- DataSet mydataset=new dataset ();
- mydataadapter=New SqlDataAdapter (Strcmd,conn);
- Mydataadapter.fill (myDataSet,"test");
- 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