在ASP.NET中連接SQL Server的簡單方法
更新時間:2013年04月28日 16:32:19 作者:
在ASP.NET中訪問SQL Server數(shù)據(jù)庫有兩種方法,它們是System.Data.OleDb和System.Data.SqlClient.下面這段程序以System.Data.SqlClient為例訪問本地數(shù)據(jù)庫服務(wù)器.
首先導(dǎo)入名字空間:System.Data和System.Data.SqlClient.詳細(xì)代碼看源程序.
<%@ 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");
//創(chuàng)建對象SqlConnection
string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";
SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
//創(chuàng)建對象SqlDataAdapter
DataSet ds = new DataSet();
//創(chuàng)建對象DataSet
myCmd.Fill(ds);
//填充數(shù)據(jù)到Dataset
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
//將數(shù)據(jù)綁定到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>
復(fù)制代碼 代碼如下:
<%@ 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");
//創(chuàng)建對象SqlConnection
string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";
SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
//創(chuàng)建對象SqlDataAdapter
DataSet ds = new DataSet();
//創(chuàng)建對象DataSet
myCmd.Fill(ds);
//填充數(shù)據(jù)到Dataset
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
//將數(shù)據(jù)綁定到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數(shù)據(jù)庫編程之Access連接失敗
- asp.net程序優(yōu)化 盡量減少數(shù)據(jù)庫連接操作
- asp.NET連接數(shù)的設(shè)置方法
- ASP.NET 6種常用數(shù)據(jù)庫的連接方法
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- ASP.NET 連接ACCESS數(shù)據(jù)庫的簡單方法
- ASP.NET連接SQL數(shù)據(jù)庫的簡單實例代碼
- ASP.NET連接MySql數(shù)據(jù)庫的2個方法及示例
- ASP.NET連接sql2008數(shù)據(jù)庫的實現(xiàn)代碼
- ASP.NET oledb連接Access數(shù)據(jù)庫的方法
- asp.net通過配置文件連接Access的方法
- asp.net快速連接access
相關(guān)文章
.NET?6開發(fā)TodoList應(yīng)用引入第三方日志庫
這篇文章主要介紹了.NET?6開發(fā)TodoList應(yīng)用引入第三方日志庫,在我們項目開發(fā)的過程中,使用.NET?6自帶的日志系統(tǒng)有時是不能滿足實際需求的,比如有的時候我們需要將日志輸出到第三方平臺,更多詳細(xì)內(nèi)容請需要的小伙伴參考下面文章內(nèi)容2021-12-12
ASP.NET數(shù)據(jù)綁定之DataList控件實戰(zhàn)篇
這篇文章主要為大家介紹了ASP.NET數(shù)據(jù)綁定中的DataList控件,DataList控件以表的形式呈現(xiàn)數(shù)據(jù),通過該控件,您可以使用不同的布局來顯示數(shù)據(jù)記錄,對DataList控件感興趣的小伙伴們可以參考一下2016-01-01
jquery中如何獲得服務(wù)器控件實現(xiàn)思路
jquery中如何獲得服務(wù)器控件,很多新手朋友對此比較陌生,接下來為您介紹解決方法,感興趣的朋友可以了解下哦2013-01-01
集合類Array List HashTable實例操作練習(xí)
集合常用操作添加、遍歷、移除;本文將詳細(xì)介紹下ArrayList對值類型的操作/ArrayList對引用類型的操作及HashTable的使用,感興趣的你可不要錯過了哈2013-02-02
利用ASP.NET MVC+Bootstrap搭建個人博客之打造清新分頁Helper(三)
這篇文章主要介紹了利用ASP.NET MVC+Bootstrap搭建個人博客之打造清新分頁Helper(三)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-06-06

