DataGrid and SQL Server 2000 Data Binding

Source: Internet
Author: User

DataGrid and SQL Server 2000 Data Binding

Select blog from powerchow

Add a DataGrid Control datagrid1 to the webform page. You only need to click a few mouse points to manually bind the DataGrid Control with sqlserver. Here isCodeBIND.
First, add the sqlclient namespace to the namespace.
Usingsystem. Data. sqlclient;
Then, create a data connection con in the page loading event,
Sqlconnectioncon = newsqlconnection ();
Con. connectionstring = "Server = (local); database = yourdatabase; trusted_connection = yes ";
Open the data connection,
Con. open ();
Create a Data command COM,
Sqlcommandcom = newsqlcommand ();
Com. commandtext = "select * fromyourtable ";
Com. Connection = con;
This allows you to start data binding in many ways. The simplest way is to directly use the executereader method of the Data command. Data Binding is to set the datasource attribute of the control.
Datagrid1.datasource = com. executereader ();
You can also use the data reader sqldatareader (cannot use its constructor). The Code is as follows:
Sqldatareaderdr = com. executereader ();
Datagrid1.datasource = Dr;
Of course, you can also use the core data set and data adapter of ADO. net;
Sqldataadapterada = newsqldataadapter (COM. commandtext, con );
Datasetset1 = newdataset ();
Ada. Fill (set1 );
Datagrid1.datasource = set1;
Finally, calculate the data binding expression,
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.