1. 程式人生 > >asp.net學習總結——ADO.net(對Sql Server進行操作的資料訪問類)

asp.net學習總結——ADO.net(對Sql Server進行操作的資料訪問類)

ADO.net物件

    System.Data.SqlClient(對Sql Server進行操作的資料訪問類):

1)SqlConnection:資料庫聯結器
2)SqlCommand:資料庫命名物件
3)SqlCommandBuilder:生存SQL命令
4)SqlDataReader:資料讀取器
5)SqlDataAdapter:資料介面卡,填充DataSet
6)SqlParameter:為儲存過程定義引數

7)SqlTransaction:資料庫事務

    下面將機房收費系統用到的資料訪問類簡略總結如下:

SqlConnection(連線物件) 

    基本語法:資料來源(DataSource)+資料庫名稱(Initial Catalog)+使用者名稱(User ID)+密碼(Password)。

  標準安全連線 

<span style="font-size:18px;">Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;或者
Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Trusted_Connection=False;</span>

  可信連線

<span style="font-size:18px;">Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;或者Server=myServerAddress;Database=myDatabase;Trusted_Connection=True; </span>

SqlCommand(命令物件

1、例項化的時候預設初始化的四個屬性:
CommandText:空字串("") 
CommandTimeout:30
CommandType:CommandType.Text
Connection:Null


2、建立命令物件:
SqlCommand command = connection.CreateCommand(); //這種方式比較好
SqlCommand command=new SqlCommand(); 


3、幾個重要的屬性:
(1)CommandText:獲取或設定要對資料來源執行的 Transact-SQL 語句、表名或儲存過程!
(2)CommandType:設定你執行的SQL語句是儲存過程還是T-SQL(是一個列舉)!
Text:SQL文字命令(預設)
StoredProcedure:儲存過程名稱
(3)Parameters:設定你T-SQL中你需要用到的引數。


4、幾個重要的方法:
(1)ExecuteNonQuery:返回是影響的行數(int),主要執行更新,新增,刪除等操作!
(2)ExecuteReader:執行SQL或儲存過程,返回的是SqlDataReader型別,主要用來查詢!
(3)ExecuteScalar:返回執行結果集中的第一行第一列,如果沒有資料,則返回NULL!

SqlParameter(Sql引數) 

1、幾個重要的屬性
ParameterName:設定引數名
Value:給引數設定值
Size:設定引數位元組最大大小
SqlDbType:引數在SQL中的型別


2、命令物件新增引數集合的幾種方法
(1)AddWithValue
(2)Add
(3)AddRange

SqlDataReader(資料流讀取器)

1、常用方法

(1)GetOrdinal:獲取指定列名的列序號(索引號),使用這個方法可以把經常變動的列進行固定
int name=dr.GetOrdinal("name"); //通過列名來獲取當前列的索引號
(2)GetName:  獲取列名,引數為指定列名的序列號,返回string
string columnName=dr.GetName(name);//通過列名所處的索引號來獲取列名名稱 
(3)IsDBNull:判斷當前讀取的資料是否為Null,返回型別為Bool
dr.IsDBNull(coContractID)?"NULL":dr.GetInt32(coContractID).ToString();
(4)NextResult:當查詢為批處理查詢時,使用這個方法去讀取下一個結果集,返回值為Bool,如果存在多個結果集,則為 true;否則為 false。
(5)Read:讀取資料


2、常用屬性
(1)HasRow:判斷是否包含一行或多行,也就是判斷有沒有資料,返回型別為Bool。
(2)FieldCount:獲取讀取的列數,返回型別為Int。
(3)IsClosed:判斷讀取的資料流是否關閉。

SqlDataAdapter(資料介面卡)

1、建構函式
四個過載:
(1)無參
(2)SqlDataAdapter(SqlCommand) 執行命令物件例項
(3)SqlDataAdapter(String,SqlConnection) 只能指定查詢語句,連線物件例項
(4)SqlDataAdapter(String,ConnectionString)


2、填充資料(Fill) 

DataSet dataSet = new DataSet();
using (SqlConnection conn = new SqlConnection(""))
{
    conn.Open();
    SqlCommand command = conn.CreateCommand();
    command.CommandText = "select name,age,address from MyInformation";
    SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
    dataAdapter.Fill(dataSet);  //填充資料
}
 
3、使用“SqlCommandBuilder”對資料進行增刪改查
(1)新增資料
using (SqlConnection conn = new SqlConnection(ConnectionString()))
{
    conn.Open();
    //構建查詢語句,也可以指定SqlCommand,其中變換的方法有很多
    SqlDataAdapter da = new SqlDataAdapter("select LastName,FirstName from dbo.Employees", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    //這句話很重要,它會把你在DataSet增加的資料轉化為SQL語句用來更新資料庫
    SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
    //新增行,例項化一個行物件,注意是用NewRow來建立行
    DataRow row = ds.Tables[0].NewRow();
    row[0] = "Yang";
    row[1] = "鬼頭";
    ds.Tables[0].Rows.Add(row);  //新增到表中
    da.Update(ds);             //把DataSet中表和資料庫進行對比,更新
} 

(2)修改資料
using (SqlConnection conn = new SqlConnection(""))
{
    SqlDataAdapter da = new SqlDataAdapter("SQL語句或你自己定義的命令物件", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
    ds.Tables[0].Rows[12][1] = ""; //修改資料
    da.Update(ds);
    //呼叫Update方法其中隱式的呼叫了AcceptChanges方法,更新資料集中的資料
    //如果你繼續使用這個資料集而沒有呼叫這個方法,在後面的使用會出現異常
    ds.AcceptChanges();  //這句話可以不寫的
} 

(3)刪除資料
using (SqlConnection conn = new SqlConnection(""))
{
    SqlDataAdapter da = new SqlDataAdapter("SQL語句或你自己定義的命令物件", conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
    //刪除資料
    ds.Tables[0].Rows[12].Delete();
    da.Update(ds);  //這邊會隱式呼叫DataTable的AcceptChanges方法
}


4、關於“SqlDataAdapter”中Fill方法
ds.Fill(ds,5,10,"MyTable");