datagrid|server| data DataGrid with SQL Server 2000 data binding
WebForm page Add a DataGrid control Datagrid1,datagrid control is manually bound with SQL Server only a few mouse clicks are done, here is code binding.
First, add the SqlClient namespace to the namespace.
Using System.Data.SqlClient;
Then create the data connection in the page Load event con,
SqlConnection con=new SqlConnection ();
Con. Connectionstring= "server= (local);d atabase=yourdatabase; Trusted_connection=yes ";
Open the data connection,
Con. Open ();
Create a data command COM,
SqlCommand com=new SqlCommand ();
com.commandtext= "SELECT * from yourtable";
Com. Connection=con;
This allows you to start data binding in a lot of ways. The simplest ExecuteReader method for directly using data commands. Data binding is the setting of the control's DataSource property.
Datagrid1.datasource=com. ExecuteReader ();
You can also use the data reader SqlDataReader (you cannot use his constructor), and the code is as follows:
SqlDataReader dr=com. ExecuteReader ();
DATAGRID1.DATASOURCE=DR;
Of course, you can also use Ado.net's core datasets and data adapters;
SqlDataAdapter Ada=new SqlDataAdapter (Com.commandtext,con);
DataSet set1=new DataSet ();
Ada. Fill (SET1);
Datagrid1.datasource=set1;
Finally, the data-binding expression is computed,
Datagrid1.databind ();