ADO.NET数据库
ASP.NET提供了ADO.NET技術(shù),它是ASP.NET應(yīng)用程序與數(shù)據(jù)庫進(jìn)行交互的一種技術(shù)。
ADO.NET技術(shù)把對(duì)數(shù)據(jù)庫的操作分為幾個(gè)步驟,并為每個(gè)步驟提供對(duì)象來封裝操作過程,從而使對(duì)數(shù)據(jù)庫的操作變得簡單易行。
ADO.NET組件通過以下兩個(gè)主要的組件將數(shù)據(jù)訪問與數(shù)據(jù)處理分離:
1.?ADO.NET體系結(jié)構(gòu)的一個(gè)核心元素是.NET數(shù)據(jù)提供程序,它是專門為數(shù)據(jù)處理以及快速地只進(jìn)、只讀訪問數(shù)據(jù)而設(shè)計(jì)的組件,包括Connection、Command、DataReader和DataAdapter對(duì)象的組件。
2.?DataSet是ADO.NET體系結(jié)構(gòu)中另一個(gè)核心組件,它是專門為各種數(shù)據(jù)源的數(shù)據(jù)訪問獨(dú)立性而設(shè)計(jì)的,所以它可以用于多個(gè)不同的數(shù)據(jù)源、XML數(shù)據(jù)或管理應(yīng)用程序的本地?cái)?shù)據(jù),如內(nèi)存中的數(shù)據(jù)高速緩存。
基本的SQL
1.選擇數(shù)據(jù)
SELECT語句語法:
SELECT?列名1,列名2,列名3,...FROM?表名(SELECT?*?FROM?表名),例如:
SELECT?*?FROM?Student
該SQL語句表示從Student表中選取所有的列。
SELECT?列名1,列名2,列名3,...FROM?表名?ORDER?BY?列名1?ASC/DESC,列名2?ASC/DESC,列名3?ASC/DESC...(ASC表示升序,DESC表示降序)
SELECT?*?FROM?Student?ORDER?BY?StuName?DESC。
該SQL語句表示從Student表中選取所有的列并按照StuName降序排序。
SELECT?列名1,列名2,列名3,...FROM?表名?WHERE子句?ORDER?BY?列名1?ASC/DESC,列名2?ASC/DESC,列名3?ASC/DESC...?(WHERE子句是條件子句,WHERE子句能夠?qū)l件進(jìn)行設(shè)置,只有滿足條件的行才會(huì)顯示到結(jié)果中)。
SELECT?*?FROM?Student?WHERE?StuClass=‘中藥一班’
有時(shí)選擇條件不止一個(gè),這時(shí)需要邏輯連接符把這些條件連接起來,常用的邏輯連接符有AND和OR,AND表示關(guān)系與,OR表示關(guān)系或。
SELECT?*?FROM?Student?WHERE?StuClass=‘中藥一班’OR?StuClass=‘中藥二班’
2.插入數(shù)據(jù)
INSERT?INTO?表名(列名1,列名2,列名3,...)?
VALUES(值1,值2,值3,...)
向Student表中添加一條數(shù)據(jù),SQL語句如下:
INSERT?INTO?Student(StuName,StuClass,StuSex)
VALUES(‘李瑩’,‘中藥三班’,‘女’)
也可以采用下面的語句實(shí)現(xiàn):
INSERT?INTO?Student
VALUES(‘李瑩’,‘中藥三班’,‘女’)
此語句忽略了列名,此時(shí)會(huì)按照列在數(shù)據(jù)表中的排列順序逐個(gè)插入。在執(zhí)行INSERT語句時(shí)值與列的數(shù)據(jù)類型必須一致,否則會(huì)報(bào)錯(cuò)。
3.更新數(shù)據(jù)
UPDATE?表名?SET
列名1=值1,
列名2=值2,
...
WHERE子句
更新Student表中的數(shù)據(jù):
UPDATE?Student?Set
StuClass=“中藥四班”,
WHERE?StuName=“李瑩”
4.刪除數(shù)據(jù)
DELETE?FROM?表名?WHRER子句
在VS中執(zhí)行數(shù)據(jù)查詢
查詢數(shù)據(jù)可以使用SQL語句實(shí)現(xiàn),也可以使用SQL?SERVER提供的管理工具來生成和測試SQl語句。在VS中執(zhí)行數(shù)據(jù)查詢的操作如下:
1.在“服務(wù)器資源管理器”窗口中,找到“表”節(jié)點(diǎn)。
2.右鍵單擊“表”節(jié)點(diǎn),在彈出的菜單中選擇“新建查詢”。
3.在彈出的“添加表”對(duì)話框中選擇要操作的表、視圖、函數(shù)、同義詞,單擊“添加”,在新建的查詢窗口中會(huì)顯示被選中的對(duì)象。
創(chuàng)建數(shù)據(jù)庫
在編寫ASP.NET網(wǎng)站應(yīng)用程序之前,首先要做的是設(shè)計(jì)和創(chuàng)建數(shù)據(jù)庫。
連接數(shù)據(jù)庫
.NET?Framework數(shù)據(jù)提供程序使用Connection對(duì)象提供與Microsoft?SQL?Server?的連接。
SqlConnection連接字符串常用參數(shù):
1.Data?Source:數(shù)據(jù)庫服務(wù)器的名稱。
2.Initial?Catalog:數(shù)據(jù)庫的名稱。
3.Integrated?Security:決定連接是否是安全的,取值可以是True、False或SSPI。
4.User?ID:SQL?Server登錄帳戶。
5.Password:SQL?Server帳戶的登錄密碼。
創(chuàng)建數(shù)據(jù)庫連接
可以通過以下方式創(chuàng)建數(shù)據(jù)庫連接:
1.使用Connection對(duì)象創(chuàng)建數(shù)據(jù)庫連接。
1 SqlConnection connection=new SqlConnection(); 2 connection.ConnectionString="server=localhost; Integrated Security=True;Initial Catalog=SuperMarket"; View Code2.使用連接字符串來初始化SqlConnection對(duì)象創(chuàng)建數(shù)據(jù)庫連接。
1 string connectionString = "Data Source=追風(fēng)的蝸牛;Initial Catalog=Adrotator;Integrated Security=True"; 2 SqlConnection connection = new SqlConnection(connectionString); View Code3.使用構(gòu)造函數(shù)來初始化SqlConnection對(duì)象創(chuàng)建數(shù)據(jù)庫連接。
?? 構(gòu)造函數(shù)
1 public class ConnectionString 2 { 3 public static string connectionString = "Data Source=追風(fēng)的蝸牛;Initial Catalog=Adrotator;Integrated Security=True"; 4 } View Code4.初始化SqlConnection對(duì)象,創(chuàng)建數(shù)據(jù)庫連接。
SqlConnection connection = new SqlConnection(ConnectionString.connectionString); View Code5.獲取在配置文件中的<connectionStrings>節(jié)中利用鍵值對(duì)存儲(chǔ)的數(shù)據(jù)庫連接字符串,創(chuàng)建數(shù)據(jù)庫連接。
利用鍵值對(duì)存儲(chǔ)數(shù)據(jù)庫連接字符串。
1 <connectionStrings > 2 <add name="connection1" connectionString="Data Source=追風(fēng)的蝸牛;Initial Catalog=Adrotator;Integrated Security=True"/> 3 </connectionStrings> View Code獲取配置文件中的數(shù)據(jù)庫連接字符串。
1 SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection1"].ConnectionString.ToString()); View Code查看連接信息
1 try 2 { 3 connection.Open(); 4 Response.Write("服務(wù)器版本:" + connection.ServerVersion.ToString()); 5 Response.Write("<br>數(shù)據(jù)庫名稱:" + connection.Database.ToString()); 6 Response.Write("<br>數(shù)據(jù)庫連接:" + connection.State.ToString()); 7 connection.Close(); 8 } 9 catch (Exception ex) 10 { 11 Response.Write("連接錯(cuò)誤!" + ex.Message.ToString()); 12 } 13 finally 14 { 15 connection.Close(); 16 } View Code獲取數(shù)據(jù)
1.Command對(duì)象
Command對(duì)象使用SELECT、INSERT、UPDATE、DELETE等SQL命令與數(shù)據(jù)源通信,此外,Command對(duì)象還可以調(diào)用存儲(chǔ)過程操作數(shù)據(jù)庫。
創(chuàng)建Command對(duì)象的方法:
1.創(chuàng)建Command對(duì)象,指定SQL命令,設(shè)置可利用的數(shù)據(jù)庫連接。
1 SqlCommand command = new SqlCommand(); 2 command.Connection = connection; 3 command.CommandText = "select * from Advertisements"; View Code
2.在創(chuàng)建Command對(duì)象時(shí)指定SQL命令和可利用的數(shù)據(jù)庫連接。
1 SqlCommand command = new SqlCommand("select * from Advertisements", connection); View Code2.DataReader對(duì)象?
可以使用DataReader從數(shù)據(jù)庫中檢索只讀、只進(jìn)的數(shù)據(jù)流。“只讀”,是指在數(shù)據(jù)閱讀器DataReader上不可更新、刪除、增加記錄。“只進(jìn)”是指記錄的接收是順序進(jìn)行且不可后退的。
數(shù)據(jù)閱讀器DataReader接收到的數(shù)據(jù)是以數(shù)據(jù)庫的記錄為單位的。查詢結(jié)果在查詢執(zhí)行時(shí)返回,并存儲(chǔ)在客戶端的網(wǎng)絡(luò)緩沖區(qū)中,直到用戶使用DataReader的Read方法對(duì)它們發(fā)出請(qǐng)求。
使用DataReader可以提高應(yīng)用程序的性能,原因是它只要數(shù)據(jù)可用就立即檢索數(shù)據(jù),并且在默認(rèn)情況下一次只在內(nèi)存中存儲(chǔ)一行,減少了系統(tǒng)開銷。
在創(chuàng)建一個(gè)Command對(duì)象的實(shí)例之后,可以通過對(duì)命令調(diào)用ExecuteReader方法來創(chuàng)建DataReader,該方法從在Command對(duì)象中指定的數(shù)據(jù)源檢索一些行,填充DataReader。
使用
1.獲取數(shù)據(jù)并綁定到控件中。
在Default.aspx中:
1 <div> 2 <asp:Label ID="Label1" Text="請(qǐng)選擇:" runat="server" /> 3 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 4 <asp:ListItem Text="請(qǐng)選擇" Enabled="true"></asp:ListItem> 5 </asp:DropDownList> 6 <br /> 7 <asp:Label ID="Label2" runat="server" Text="廣告的詳細(xì)信息:" /> 8 <br /> 9 <asp:Label ID="Label3" runat="server" Height="200" Width="300" /> 10 </div> View Code在Default.aspx.sc中:
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!this.IsPostBack) 4 { 5 SqlCommand command = new SqlCommand("select AlternateText,ID from Advertisements", connection); 6 SqlDataReader dataReader; 7 try 8 { 9 connection.Open(); 10 dataReader = command.ExecuteReader(); 11 12 while (dataReader.Read()) 13 { 14 ListItem listItem = new ListItem(); 15 listItem.Text = dataReader["AlternateText"].ToString(); 16 listItem.Value = dataReader["ID"].ToString(); 17 DropDownList1.Items.Add(listItem); 18 } 19 dataReader.Close(); 20 } 21 catch (Exception ex) 22 { 23 Label3.Text = "讀取失敗" + ex.Message.ToString(); 24 } 25 finally 26 { 27 connection.Close(); 28 } 29 } 30 } View Code2.獲取控件數(shù)據(jù)信息并查詢。
在Default.aspx.sc中:
1 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection1"].ConnectionString.ToString()); 4 string selectSql = "select * from Advertisements where ID='" + DropDownList1.SelectedItem.Value + "'";//定義SQL語句獲取廣告信息 5 6 SqlCommand command = new SqlCommand(selectSql, connection); 7 SqlDataReader dataReader; 8 try 9 { 10 connection.Open(); 11 dataReader = command.ExecuteReader(); 12 dataReader.Read(); 13 string stringText; 14 stringText ="ID:"+ dataReader["ID"].ToString() + "<br>" +"圖片地址:"+ dataReader["ImageUrl"].ToString() + "<br>" +"鏈接地址:"+ dataReader["NavigateUrl"].ToString() + "<br>" +"顯示頻率:"+ dataReader["Impressions"].ToString() + "<br>" +"描述信息:"+ dataReader["AlternateText"].ToString(); 15 Label3.Text = stringText.ToString(); 16 dataReader.Close(); 17 18 } 19 catch (Exception ex) 20 { 21 Label3.Text = "讀取失敗" + ex.Message.ToString(); 22 } 23 finally 24 { 25 connection.Close(); 26 } 27 } View Code3.DataAdapter對(duì)象
DataAdapter對(duì)象充當(dāng)數(shù)據(jù)庫和ADO.NET對(duì)象模型中非連接對(duì)象之間的橋梁,能夠用來保存和檢索數(shù)據(jù)。DataAdapter對(duì)象類的Fill方法用于將查詢結(jié)果引入DataSet或DataTable中,以便能夠脫機(jī)處理數(shù)據(jù)。根據(jù)不同的數(shù)據(jù)源DataAdapter對(duì)象,可以分為四類:
1.SqlDataAdapter:用于對(duì)SQL?Server數(shù)據(jù)庫執(zhí)行命令。
2.OleDBDataAdapter:用于對(duì)支持OleDB的數(shù)據(jù)庫執(zhí)行命令。
3.OdbcDataAdapter:用于支持Odbc的數(shù)據(jù)庫執(zhí)行命令。
4.OracleDataAdapter:用于對(duì)Oracle數(shù)據(jù)庫執(zhí)行命令。
使用SqlDataAdapter的步驟:
1.?建立數(shù)據(jù)庫連接。
2.?建立SqlCommand對(duì)象,設(shè)置要執(zhí)行的SQL語句
3.?建立并實(shí)例化一個(gè)SqlDataAdapter對(duì)象。
4.?建立一個(gè)DataSet對(duì)象,用于接收?qǐng)?zhí)行SQL命令返回的數(shù)據(jù)集。
5.?填充數(shù)據(jù)集。
6.?綁定數(shù)據(jù)控件。
7.?關(guān)閉數(shù)據(jù)庫連接。
4.DataSet對(duì)象
DataSet在ADO.NET對(duì)實(shí)現(xiàn)從數(shù)據(jù)庫抽取數(shù)據(jù)起到關(guān)鍵作用,數(shù)據(jù)從數(shù)據(jù)庫完成數(shù)據(jù)抽取后存放在DataSet中,是各種數(shù)據(jù)源中的數(shù)據(jù)在計(jì)算機(jī)內(nèi)存中映射成的緩存,從某種意義上可以把DataSet可以看成是一個(gè)數(shù)據(jù)容器。
DataSet也被稱為內(nèi)存中的數(shù)據(jù)庫,因?yàn)樵贒ataSet可以包含很多數(shù)據(jù)表以及數(shù)據(jù)表之間的關(guān)系。
DataSet在客戶端實(shí)現(xiàn)讀取、更新數(shù)據(jù)庫等過程中起到了中間部件的作用。
DataSet從數(shù)據(jù)源中獲取數(shù)據(jù)后就會(huì)斷開與數(shù)據(jù)源之間的連接。允許在DataSet中定義數(shù)據(jù)約束和表關(guān)系,增加、刪除和編輯記錄,還可以對(duì)DataSet中的數(shù)據(jù)進(jìn)行查詢、統(tǒng)計(jì)等。當(dāng)完成了各項(xiàng)操作以后還可以把DataSet中的數(shù)據(jù)送回?cái)?shù)據(jù)源。DataSet的產(chǎn)生滿足了多層分布式程序的需要,它能夠在斷開數(shù)據(jù)源的情況下對(duì)存放在內(nèi)存中的數(shù)據(jù)進(jìn)行操作,這樣可以提高系統(tǒng)整體性能,而且有利于擴(kuò)展。
創(chuàng)建DataSet的方式
1.建立一個(gè)空的數(shù)據(jù)集,然后再把建立的數(shù)據(jù)表放到該數(shù)據(jù)集里。
DataSet?dataSet?=?new?DataSet();
2.先建立數(shù)據(jù)表,然后再建立包含數(shù)據(jù)表的數(shù)據(jù)集。
DataSet?dataSet?=?new?DataSet(“表名”);
DataSet中經(jīng)常使用的類
1.DataTable。稱為數(shù)據(jù)表,用來存放數(shù)據(jù)。一個(gè)Dataset對(duì)象可以包含多個(gè)DataTable,每個(gè)DataTable可以包含多個(gè)DataRow和DataColumn。
它的創(chuàng)建方式有兩種:
1.數(shù)據(jù)加載DataSet時(shí)會(huì)自動(dòng)創(chuàng)建DataTable。
2.以編程方式創(chuàng)建DataTable的對(duì)象,然后將對(duì)象添加到DataSet的Tables集合中。
從DataSet中提取DataTable
DataSet?dataset=new?DataSet();
DataTable?dataTable=dataset.數(shù)據(jù)表名
2.DataRow。數(shù)據(jù)表里的行,是給定DataTable中的一行數(shù)據(jù)(記錄)。DataRow對(duì)象的方法提供了對(duì)表中數(shù)據(jù)的增加、刪除、更新、查詢等功能。
提取表中的一行數(shù)據(jù)(記錄)
DataRow?dataRow=DataTable.Row[n];
3.DataColumn。數(shù)據(jù)表中的列,定義了數(shù)據(jù)表的數(shù)據(jù)結(jié)構(gòu)。
獲取某列的值需要在數(shù)據(jù)行的基礎(chǔ)上進(jìn)行
string?str=dataRow.Column[“字段名稱”].ToString();
?? 或string?str=dataRow.Column[“索引”].ToString();
4.DataRelation。
使用DataRelation通過DataColumn對(duì)象將兩個(gè)DataTable對(duì)象相互關(guān)聯(lián),此類關(guān)系類似于關(guān)系數(shù)據(jù)庫中的主鍵/外鍵關(guān)系。關(guān)系是在父表和子表中匹配的列之間創(chuàng)建的,兩個(gè)列的數(shù)據(jù)類型必須相同。
創(chuàng)建DataRelation時(shí),首先要驗(yàn)證是否可以創(chuàng)建關(guān)系。在將它添加到DataRelationCollection之后,通過禁止會(huì)使關(guān)系無效的任何更改來維持此關(guān)系。在創(chuàng)建DataRelation和將其添加到DataRelationCollection之間的這段時(shí)間,可以對(duì)父行或子行進(jìn)行其他更改,但是會(huì)使關(guān)系失效,會(huì)產(chǎn)生異常。
使用
在配置文件中添加以下內(nèi)容:
1 <connectionStrings > 2 <add name="connection1" connectionString="Data Source=追風(fēng)的蝸牛;Initial Catalog=Adrotator;Integrated Security=True"/> 3 </connectionStrings> View Code在Default.aspx.cs中添加以下內(nèi)容:
1 <div> 2 <asp:Label ID="Label1" Text="請(qǐng)選擇:" runat="server" /> 3 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 4 <asp:ListItem Text="請(qǐng)選擇" Enabled="true"></asp:ListItem> 5 </asp:DropDownList> 6 <br /> 7 <asp:Label ID="Label2" runat="server" Text="廣告的詳細(xì)信息:" /> 8 <br /> 9 <asp:Label ID="Label3" runat="server" Height="200" Width="300" /> 10 </div> View CodeDefault.aspx.cs中的代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Text; 7 using System.Web; 8 using System.Web.UI; 9 using System.Web.UI.WebControls; 10 11 namespace WebApplication1 12 { 13 public partial class Default : System.Web.UI.Page 14 { 15 private static DataSet dataSet = new DataSet(); 16 public class ConnectionString 17 { 18 public static string connectionString = "Data Source=追風(fēng)的蝸牛;Initial Catalog=Adrotator;Integrated Security=True"; 19 } 20 21 protected void Page_Load(object sender, EventArgs e) 22 { 23 if (!this.IsPostBack) 24 { 25 fillList(); 26 } 27 } 28 29 protected void fillList() 30 { 31 SqlConnection connection = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connection1"].ConnectionString.ToString()); 32 SqlCommand command = new SqlCommand("select * from Advertisements", connection); 33 SqlDataAdapter dataAdapter = new SqlDataAdapter(command); 34 //打開數(shù)據(jù)庫連接,讀取信息 35 try 36 { 37 connection.Open(); 38 //填充Dataset 39 dataAdapter.Fill(dataSet, "Advertisements"); 40 } 41 catch (Exception ex) 42 { 43 Label3.Text = "讀取錯(cuò)誤" + ex.Message.ToString(); 44 } 45 finally 46 { 47 connection.Close(); 48 } 49 foreach (DataRow row in dataSet.Tables["Advertisements"].Rows) 50 { 51 ListItem item = new ListItem(); 52 item.Text = row["AlternateText"].ToString(); 53 item.Value = row["ID"].ToString(); 54 DropDownList1.Items.Add(item); 55 } 56 } 57 58 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 59 { 60 try 61 { 62 DataRow[] dataRow = dataSet.Tables["Advertisements"].Select("ID=" + DropDownList1.SelectedItem.Value); 63 string stringText = "ID:" + dataRow[0]["ID"].ToString() + "<br/>圖片地址:" + dataRow[0]["ImageUrl"].ToString() + "<br/>鏈接地址:" + dataRow[0]["NavigateUrl"].ToString() + "<br/>顯示頻率:" + dataRow[0]["Impressions"].ToString() + "<br/>描述:" + dataRow[0]["AlternateText"].ToString(); 64 Label3.Text = stringText.ToString(); 65 } 66 catch (Exception ex) 67 { 68 Label3.Text = "讀取失敗" + ex.Message.ToString(); 69 } 70 } 71 } 72 } View Code
?
轉(zhuǎn)載于:https://www.cnblogs.com/spilledlight/p/4862700.html
總結(jié)
以上是生活随笔為你收集整理的ADO.NET数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AXURE 8.1.0.3382 有效激
- 下一篇: sama5d3 环境检测 gpio--y