盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

在ASP.NET中访问SQL Server

时间:2024/10/25作者:未知来源:盾怪网教程人气:

[摘要]在ASP.NET中访问SQL Server数据库有两种方法,它们是System.Data.OleDb和System.Data.SqlClient.下面这段程序以System.Data.SqlClie...

在ASP.NET中访问SQL Server数据库有两种方法,它们是System.Data.OleDb和System.Data.SqlClient.下面这段程序以System.Data.SqlClient为例访问本地数据库服务器.
首先导入名字空间: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中访问SQL Server




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版