国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.NET2.0數(shù)據(jù)庫(kù)入門之SqlDataSource

2024-07-10 13:10:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  當(dāng)使用sqldatasource控件選擇數(shù)據(jù)時(shí),可以從兩個(gè)屬性:connectionstring和selectcommand開(kāi)始,如下所示:

<asp:sqldatasource id="mysourcecontrolname" runat="server"
connectionstring="server=myserver ;
database=northwind"
selectcommand=" select fieldl, [field with space] from mytable">
</asp:sgldatasource>

  當(dāng)使用windows認(rèn)證時(shí),可以添加兩個(gè)認(rèn)證數(shù)據(jù)。

<asp:sqldatasource id="mysourcecontrolname" runat="server"
connectionstring="
server=myserver ;
user id=myid;
password=mypass;
database=northwind"
selectcommand=" select field1, [field with space] from mytable">
</asp:sqldatasource>

  第一個(gè)是前面討論過(guò)的連接字符串,第二個(gè)是selectcommand,用來(lái)確定將從sql server數(shù)據(jù)庫(kù)中提取什么信息。在selectcommand中,可以使用任何符合語(yǔ)法的sql select語(yǔ)句,包括在上一章或者附錄a中討論的語(yǔ)句。很多sql server管理員都不會(huì)允許用戶直接訪問(wèn)表。數(shù)據(jù)庫(kù)管理員將會(huì)在sproc上創(chuàng)建一些受限制的許可。或者,創(chuàng)建表的視圖用來(lái)提供表的一部分?jǐn)?shù)據(jù)或者對(duì)表中可以修改的數(shù)據(jù)進(jìn)行限制。連接至視圖的語(yǔ)法如下:

selectcommand="select * from myview">

  如果表、查詢、sproc或者視圖的名稱中有空格字符,則應(yīng)當(dāng)使用方括號(hào)將整個(gè)名稱包含起來(lái),如下所示:

selectcommand="select * from [my view] "

  您可能已經(jīng)注意到了gridview中的filter屬性并且想知道它與在數(shù)據(jù)源selectcommand中使用where子句之間有何不同。篩選只用于某些緩沖情況,我們將會(huì)在第15章中介紹。

  有了連接字符串和selectcommand,就可以創(chuàng)建從sql server使用數(shù)據(jù)的頁(yè)面了。

  試一試#1—— sqldatasource簡(jiǎn)單示例

  在本練習(xí)中,應(yīng)該以網(wǎng)格(表)格式從northwind的sql版本顯示產(chǎn)品的gridview。可以以添加datasource控件和數(shù)據(jù)綁定控件的技術(shù)開(kāi)始,這樣就能夠創(chuàng)建最簡(jiǎn)單的源代碼。然后將會(huì)使用一種更快速的開(kāi)發(fā)技術(shù)(拖放列名稱)。

  (1) 請(qǐng)確認(rèn)已經(jīng)安裝了sse(如第1章所述),包括樣本數(shù)據(jù)庫(kù)northwind。本練習(xí)還將涉及到sql server或者msde。

  (2) 創(chuàng)建文件夾ch03,在其中創(chuàng)建一個(gè)名為tio-1-sqlsimple-1.aspx的文件。通過(guò)menu:view|toolbox(ctrl+alt+x)顯示工具箱。請(qǐng)注意工具箱有一個(gè)可以展開(kāi)的data區(qū)域。

  (3) 在design視圖中,從工具箱的data區(qū)域中拖放一個(gè)sqldatasource控件至頁(yè)面。在便捷任務(wù)面板上,將數(shù)據(jù)源配置為新的連接。輸入服務(wù)器名稱(local)/sqlexpress并使用windows nt認(rèn)證。選擇名為northwind的數(shù)據(jù)庫(kù)并測(cè)試連接。單擊ok結(jié)束。您將會(huì)自動(dòng)返回至data source configuration對(duì)話框,單擊next。對(duì)本例來(lái)說(shuō),不需要在配置文件中保存連接字符串;單擊next。選擇“specify columns from a table”并選擇表名products。在columns列表中,單擊id、name和unit price。單擊next和test query,然后單擊finish。這樣就完成添加datasource控件了。

  (4) 添加gridview數(shù)據(jù)綁定控件。在便捷任務(wù)面板中,選擇sqldatasource,然后關(guān)閉便捷任務(wù)面板。這樣就創(chuàng)建了數(shù)據(jù)綁定控件。保存并運(yùn)行頁(yè)面,頁(yè)面如下:

<%@ page language="vb" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en"
"http://www.w3.org/tr/xhtmlll/dtd/xhtmlll.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ch03-tio#l-sqlsimple-verl</title>
</head>
<body>
<h2>
chapter 3 tio #1 sqlsimple verl
</h2>
<form id="forml" runat="server">
<div>
<asp:sqldatasource id="sqldatasourcel" runat="server"
providername="system.data.sqlclient"
connectionstring="server=(local)/sqlexpress;
integrated security=true;
database=northwind;
persist security info=true"
selectcommand="select [productid], [productname], [unitprice] from[products]">
</asp:sqldatasource>
<asp:gridview id="gridviewl" runat="server"
datasourceid="sqldatasourcel"
datakeynames="productid"
autogeneratecolumns="false">
<columns>
<asp:boundfield readonly="true" headertext="productid"
insertvisible="false" datafield="productid"
sortexpression=" productid" ></asp: boundfield>
<asp:boundfield headertext="productname"datafield="productname"
sortexpression="productname"></asp:boundfield>
<asp:boundfield headertext="unitprice" datafield="unitprice"
sortexpression="unitprice"></asp:boundfield>
</columns>
</asp:gridview>
</div>
</form>
</body>
</html>

  (5) 關(guān)閉瀏覽器并查看一下source視圖中的頁(yè)面。請(qǐng)注意,在頁(yè)面的<form>中間有兩個(gè)控件。sqldatasource有connectionstring和selectcommand。gridview具有一些與sqldatasource控件的字段綁定的列。

  (6) 現(xiàn)在可以使用快速的技術(shù)添加第二個(gè)表了。將頁(yè)面保存為tio-1-sqlsim- ple-2.aspx。切換至design視圖。在菜單中,依次單擊view->database explorer。在data connections上單擊右鍵并選擇add connection。與上一步驟一樣,您將面對(duì)連接屬性對(duì)話框。輸入服務(wù)器名稱(local)/sqlexpress,使用windows nt安全,并選擇名為northwind的數(shù)據(jù)庫(kù)。測(cè)試連接并單擊ok以關(guān)閉對(duì)話框。請(qǐng)注意在數(shù)據(jù)庫(kù)瀏覽窗口中的新項(xiàng)目。

  (7) 展開(kāi)新的(local)/sqlexpress.northwind.dbo連接,然后展開(kāi)它的表。展開(kāi)categories表。使用ctrl+單擊的方式選擇categoryid、categoryname和description字段并將它們拖放至頁(yè)面的底部。雖然便捷任務(wù)面板將為您打開(kāi)新的gridview,但是可以關(guān)閉它。鍵入f5來(lái)運(yùn)行頁(yè)面。向下滾動(dòng)欣賞您僅僅通過(guò)從category表拖放列名就創(chuàng)建出來(lái)的頁(yè)面。以下是版本2的其他源代碼。請(qǐng)注意,當(dāng)拖放列名的時(shí)候,您會(huì)得到一大組代碼,包括在sqldatasource控件中的一系列參數(shù)。

...

<asp:sqldatasource id="sqldatasourcel" runat="server"
<asp:gridview id="gridviewl" runat="server"
...

<asp:gridview id="gridview2" runat="server"
datasourceid="sqldatasource2" datakeynames="categoryid"
autogeneratecolumns="false"
emptydatatext="there are no data records to display.">
<columns>
<asp:boundfield readonly="true" headertext="categoryid"
datafield="categoryid"
sortexpression="category id"></asp:boundfield>
<asp:boundfield headertext="categoryname"datafield="categoryname"
sortexpression="categorymame"></asp:boundfield>
<asp:boundfield
headertext="description" datafield="description"
sortexpression="description"></asp:boundfield>
</columns>
</asp:gridview>
<asp:sqldatasource id="sqldatasource2" runat="server"
providername="<%$ connectionstrings:appconnectionstring2 . providername %>"
connectionstring="<%$ connectionstrings:appconnectionstring2 %>"
selectcommand="select[categoryid],[categoryname] ,[desc- ription] from
[categories]"
updatecommand="update[categories]set[categoryname][email protected],
[description] = @description where[categoryid][email protected]_categoryid"
insertcommand="insert into[categories]([categoryname],[description]
values (@categoryname, @description)"
deletecommand="delete from [categories] where [categoryid] =
@original_categoryid">
<deleteparameters>
<asp:parameter type="int32" name="categoryid"></asp:parameter>
</deleteparameters>
<insertparameters>
<asp:parameter type="string"name="categoryname"></asp:parameter>
<asp: parameter type="string" name="description"></asp: parameter>
</insertparameters>
<updateparameters>
<asp:parameter type="string" name="categoryname"></asp:parameter>
<asp:parametertype="string" name="description"></asp:parameter>
<asp:parameter type="int32"name="categoryid"></asp:parameter>
</updateparameters>
</asp:sqldatasource>

  示例說(shuō)明#1—— sqldatasource簡(jiǎn)單示例

  對(duì)語(yǔ)法來(lái)說(shuō),sqldatasource與我們?cè)诘?章所使用的accessdatasource相差無(wú)幾。最大的差別就是連接字符串的使用(而不是指定數(shù)據(jù)文件)。兩者相似的意義是對(duì)一種控件的知識(shí)可以幫助了解其他的數(shù)據(jù)源控件。

  連接字符串向sqldatasource傳遞了三個(gè)參數(shù):

  ● sql server的名稱

  ● 將要使用的數(shù)據(jù)庫(kù)的名稱或者視圖的名稱
  
  ● 指示在隨后的sse登錄中持續(xù)使用windows認(rèn)證

  不論是對(duì)表還是視圖來(lái)說(shuō),如果其名稱中包含空格,就必須將名稱包括在方括號(hào)之中。第二個(gè)參數(shù)提供了一個(gè)符合標(biāo)準(zhǔn)sql語(yǔ)法的選擇命令。

  當(dāng)我們轉(zhuǎn)移至版本2時(shí),使用了vwd的完全功能。通過(guò)添加連接至database explorer,我們讓vwd明白數(shù)據(jù)庫(kù)的模式。vwd將會(huì)顯示表和列名。在拖放列名至頁(yè)面時(shí),vwd將創(chuàng)建包含了支持的datasource控件在內(nèi)的gridview。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 易门县| 监利县| 额尔古纳市| 沙坪坝区| 陆良县| 巴林左旗| 云和县| 晋州市| 百色市| 始兴县| 吉首市| 通辽市| 乡宁县| 焉耆| 丰城市| 东宁县| 芒康县| 永城市| 屏南县| 从江县| 皮山县| 平江县| 九龙县| 封丘县| 望奎县| 阿克| 日土县| 息烽县| 健康| 南通市| 称多县| 赤水市| 台中市| 尼木县| 寻甸| 赤水市| 福州市| 六盘水市| 利津县| 钟山县| 内江市|