首先导入名字空间:System.Data和System.Data.SqlClient.详细代码看源程序.
复制代码 代码如下:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
protected void Page_Load(Object Src, EventArgs E )
{
SqlConnection myConn = new SqlConnection("server=localhost;uid=sa;pwd=;database=pubs");
//创建对象SqlConnection
string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";
SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
//创建对象SqlDataAdapter
DataSet ds = new DataSet();
//创建对象DataSet
myCmd.Fill(ds);
//填充数据到Dataset
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
//将数据绑定到DataGrid
}
</script>
<body>
<h3><font face="Verdana">Simple SELECT to a DataGrid Control
</font></h3>
<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>
</html>
您可能感兴趣的文章:
- ASP.NET数据库编程之Access连接失败
- asp.net程序优化 尽量减少数据库连接操作
- asp.NET连接数的设置方法
- ASP.NET 6种常用数据库的连接方法
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)
- ASP.NET 连接ACCESS数据库的简单方法
- ASP.NET连接SQL数据库的简单实例代码
- ASP.NET连接MySql数据库的2个方法及示例
- ASP.NET连接sql2008数据库的实现代码
- ASP.NET oledb连接Access数据库的方法
- asp.net通过配置文件连接Access的方法
- asp.net快速连接access
本文由 华域联盟 原创撰写:华域联盟 » 在ASP.NET中连接SQL Server的简单方法
转载请保留出处和原文链接:https://www.cnhackhy.com/49961.htm