在ASP.NET中連接SQL Server的簡(jiǎn)單方法
更新時(shí)間:2013年04月28日 16:32:19 作者:
在ASP.NET中訪(fǎng)問(wèn)SQL Server數(shù)據(jù)庫(kù)有兩種方法,它們是System.Data.OleDb和System.Data.SqlClient.下面這段程序以System.Data.SqlClient為例訪(fǎng)問(wèn)本地?cái)?shù)據(jù)庫(kù)服務(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)建對(duì)象SqlConnection
string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";
SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
//創(chuàng)建對(duì)象SqlDataAdapter
DataSet ds = new DataSet();
//創(chuàng)建對(duì)象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)建對(duì)象SqlConnection
string strSQL="SELECT au_id,au_lname,au_fname,phone,address,city,zip FROM authors";
SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
//創(chuàng)建對(duì)象SqlDataAdapter
DataSet ds = new DataSet();
//創(chuàng)建對(duì)象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ù)庫(kù)編程之Access連接失敗
- asp.net程序優(yōu)化 盡量減少數(shù)據(jù)庫(kù)連接操作
- asp.NET連接數(shù)的設(shè)置方法
- ASP.NET 6種常用數(shù)據(jù)庫(kù)的連接方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- ASP.NET 連接ACCESS數(shù)據(jù)庫(kù)的簡(jiǎn)單方法
- ASP.NET連接SQL數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例代碼
- ASP.NET連接MySql數(shù)據(jù)庫(kù)的2個(gè)方法及示例
- ASP.NET連接sql2008數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
- ASP.NET oledb連接Access數(shù)據(jù)庫(kù)的方法
- asp.net通過(guò)配置文件連接Access的方法
- asp.net快速連接access
相關(guān)文章
Asp.net MVC scheduler的實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Asp.net MVC scheduler的實(shí)現(xiàn)方法詳解的相關(guān)資料,希望通過(guò)本文大家能夠?qū)崿F(xiàn)這樣的方法,需要的朋友可以參考下2017-10-10asp.net線(xiàn)程批量導(dǎo)入數(shù)據(jù)時(shí)通過(guò)ajax獲取執(zhí)行狀態(tài)
asp.net線(xiàn)程批量導(dǎo)入數(shù)據(jù)是大家日常工作中常遇到的一個(gè)要求,但批量添加時(shí)間一般較長(zhǎng),如果能返回執(zhí)行的狀態(tài)就好,那么下面這篇文章主要給大家介紹了asp.net線(xiàn)程批量導(dǎo)入數(shù)據(jù)時(shí)通過(guò)ajax獲取執(zhí)行狀態(tài)的方法,有需要的朋友可以參考下。2016-12-12ASP.NET返回上一頁(yè)面的實(shí)現(xiàn)代碼
今天用ASP.NET做一個(gè)類(lèi)似淘寶中的訂單管理系統(tǒng),遇到的問(wèn)題:主界面通過(guò)一個(gè)超鏈接跳轉(zhuǎn)到子界面后,試了好長(zhǎng)時(shí)間還是無(wú)法從子界面返回主界面。2011-04-04IE下document.referrer 拒絕訪(fǎng)問(wèn)的解決方法
原理就是給IE瀏覽器的頁(yè)面偷偷加了個(gè)鏈接,然后自動(dòng)點(diǎn)這個(gè)鏈接,于是referrer就能保留了,感興趣的朋友可以參考下2013-09-09MVC+Bootstrap+Drapper使用PagedList.Mvc支持多查詢(xún)條件分頁(yè)
這篇文章主要介紹了MVC+Bootstrap+Drapper使用PagedList.Mvc支持多查詢(xún)條件分頁(yè),需要的朋友可以參考下2017-05-05基于ASP.NET+easyUI框架實(shí)現(xiàn)圖片上傳功能(判斷格式+即時(shí)瀏覽 )
這篇文章主要介紹了基于ASP.NET+easyUI框架實(shí)現(xiàn)圖片上傳功能的相關(guān)資料,重點(diǎn)在于如何判斷格式,實(shí)現(xiàn)即時(shí)瀏覽,需要的朋友可以參考下2016-06-06