Accessing SQL Server in asp.net

Source: Internet
Author: User
asp.net|server| Access There are two ways to access a SQL Server database in asp.net, which are System.Data.OleDb and System.Data.SqlClient. The following procedure takes System.Data.SqlClient as an example to access the local database server.
First, you import namespaces: System.Data and System.Data.SqlClient. Detailed code to see the source program.
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "C #" runat= "Server" >
protected void Page_Load (Object Src, EventArgs E)
{
SqlConnection myconn = new SqlConnection ("server=localhost;uid=sa;pwd=;d atabase=pubs");
Create Object SqlConnection
String strsql= "Select Au_id,au_lname,au_fname,phone,address,city,zip from Authors";
SqlDataAdapter mycmd = new SqlDataAdapter (strSQL, myconn);
Create Object SqlDataAdapter
DataSet ds = new DataSet ();
Create Object DataSet
Mycmd.fill (DS);
Populating data to a dataset
DataView Source = new DataView (ds. Tables[0]);
Mydatagrid.datasource = source;
Mydatagrid.databind ();
Binding Data to a DataGrid
}
</script>
<body>
</font><asp:datagrid id= "Mydatagrid" runat= "Server"
Width= "600"
Backcolor= "#ccccff"
Bordercolor= "BLACK"
Showfooter= "false"
Cellpadding=3
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Maintainstate= "false"
/>
</body>


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.