ado.net提供了豐富的數(shù)據(jù)庫操作,這些操作可以分為三個步驟:
第一拄显,使用SqlConnection對象連接數(shù)據(jù)庫苟径;
第二,建立SqlCommand對象躬审,負責SQL語句的執(zhí)行和存儲過程的調(diào)用棘街;
第三蟆盐,對SQL或存儲過程執(zhí)行后返回的“結(jié)果”進行操作。
對返回“結(jié)果”的操作可以分為兩類:
一是用SqlDataReader直接一行一行的讀取數(shù)據(jù)集蹬碧;
二是DataSet聯(lián)合SqlDataAdapter來操作數(shù)據(jù)庫。
兩者比較:
SqlDataReader時刻與遠程數(shù)據(jù)庫服務器保持連接炒刁,將遠程的數(shù)據(jù)通過“流”的形式單向傳輸給客戶端恩沽,它是“只讀”的。由于是直接訪問數(shù)據(jù)庫翔始,所以效率較高罗心,但使用起來不方便。
DataSet一次性從數(shù)據(jù)源獲取數(shù)據(jù)到本地城瞎,并在本地建立一個微型數(shù)據(jù)庫(包含表渤闷、行、列脖镀、規(guī)則飒箭、表之間的關系等),期間可以斷開與服務器的連接蜒灰,使用SqlDataAdapter對象操作“本地微型數(shù)據(jù)庫”弦蹂,結(jié)束后通過SqlDataAdapter一次性更新到遠程數(shù)據(jù)庫服務器。這種方式使用起來更方强窖,便簡單凸椿。但性能較第一種稍微差一點。(在一般的情況下兩者的性能可以忽略不計翅溺。)
一.通過ConfigurationManager.ConnectionStrings獲取存儲在webconfig中數(shù)據(jù)庫的配置信息脑漫,進行數(shù)據(jù)庫鏈接
需要引用using System.Web.Configuration命名空間
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public static void SQLConn()
{
//獲取數(shù)據(jù)庫鏈接信息
String connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//鏈接數(shù)據(jù)庫
SqlConnection conn = new SqlConnection(connStr);
//打開數(shù)據(jù)庫鏈接
Conn.Open();
SqlCommand cmd = new SqlCommand("select * from stocklog", Conn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
通過string connStr=ConfigurationManager.ConnectionString[“connStr”].ConnectionString;獲得數(shù)據(jù)庫連接的一些配置信息,即“數(shù)據(jù)源”咙崎、“數(shù)據(jù)庫名”优幸、“用戶名”、“密碼”褪猛,將這些信息都保存在connStr中劈伴。
webconfig中配置如下:
<configuration>
<connectionStrings>
<add name="connStr" connectionString="Server=.;DataBase=;Uid=;Pwd=">
</connectionStrings>
</configuration>
二.利用Connection類對象鏈接數(shù)據(jù)庫
Connection類有四種:SqlConnection,OleDbConnection握爷,OdbcConnection和OracleConnection跛璧。
SqlConnection類的對象連接SQL Server數(shù)據(jù)庫;OracleConnection 類的對象連接Oracle數(shù)據(jù)庫新啼;OleDbConnection類的對象連接支持OLEDB的數(shù)據(jù)庫追城,如Access;而OdbcConnection類的對象連接任何支持ODBC的數(shù)據(jù)庫燥撞。與數(shù)據(jù)庫的所有通訊最終都是通過Connection對象來完成的座柱。
SqlConnection conn = new SqlConnection("Server=.;DataBase=;Uid=;Pwd=");
conn.Open();
注意:使用不同的Connection對象需要導入不同的命名空間迷帜。OleDbConnection的命名空間為System.Data.OleDb。SqlConnection的命名空間為System.Data.SqlClient色洞。OdbcConnection的命名空間為System.Data.Odbc戏锹。OracleConnection的命名空間為System.Data.OracleClinet。
1火诸、集成的Windows身份驗證語法范例
string constr = "server=.;database=myschool;integrated security=SSPI";
說明:程序代碼中锦针,設置了一個針對Sql
Server數(shù)據(jù)庫的連接字符串。其中server表示運行Sql
Server的計算機名置蜀,由于程序和數(shù)據(jù)庫系統(tǒng)是位于同一臺計算機的奈搜,所以我們可以用.(或localhost)取代當前的計算機名。database表示所使用的數(shù)據(jù)庫名(myschool)盯荤。由于我們希望采用集成的Windows驗證方式馋吗,所以設置
integrated security為SSPI即可。
2秋秤、Sql Server 2005中的Windows身份驗證模式如下:
string constr = "server=.;database=myschool;uid=sa;pwd=sa";
說明:程序代碼中宏粤,采用了使用已知的用戶名和密碼驗證進行數(shù)據(jù)庫的登錄。數(shù)據(jù)庫連接字符串是不區(qū)分大小寫的灼卢。uid為指定的數(shù)據(jù)庫用戶名商架,pwd為指定的用戶口令。為了安全起見芥玉,一般不要在代碼中包括用戶名和口令蛇摸,你可以采用前面的集成的Windows驗證方式或者對Web.Config文件中的連接字符串加密的方式提高程序的安全性。
3灿巧、Sql Server 2005中的Sql Server身份驗證模式如下:
string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";
說明:程序代碼中data source 表示運行數(shù)據(jù)庫對應的計算機名赶袄,initial catalog表示所使用的數(shù)據(jù)庫名。uid為指定的數(shù)據(jù)庫用戶名抠藕,pwd為指定的用戶口令饿肺。
SqlCommand對象(負責執(zhí)行sql語句或存儲過程的調(diào)用)
命名空間:System.Data.SqlClient.SqlCommand;
SqlCommand對象用于執(zhí)行數(shù)據(jù)庫操作,操作方式有三種:
SQL語句:command.CommandType = CommandType.Text;
存儲過程:command.CommandType = CommandType.StoredProcedure;
整張表:command.CommandType = CommandType.TableDirect;
實例化一個SqlCommand對象
SqlCommand command = new SqlCommand();
command.Connection = sqlCnt;? ? ? ? ? ? // 綁定SqlConnection對象
或直接從SqlConnection創(chuàng)建
SqlCommand command = sqlCnt.CreateCommand();
常用方法:
command.ExecuteNonQuery(): 返回受影響函數(shù),如增、刪达传、改操作;
command.ExecuteScalar():執(zhí)行查詢溉跃,返回首行首列的結(jié)果;
command.ExecuteReader():返回一個數(shù)據(jù)流(SqlDataReader對象)告抄。
常用操作
① 執(zhí)行SQL
SqlCommand cmd = conn.CreateCommand();? ? ? ? ? ? ? //創(chuàng)建SqlCommand對象
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from products = @ID";? //sql語句
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = 1;? ? ? ? ? ? ? ? ? ? //給參數(shù)sql語句的參數(shù)賦值
② 調(diào)用存儲過程
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "存儲過程名";
③ 整張表
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.TableDirect;
cmd.CommandText = "表名"