DataGrid with SQL Server 2000 data binding

Source: Internet
Author: User
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 ();



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.