欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中的SQLCommand命令與DbTransaction事務(wù)處理

 更新時(shí)間:2022年05月11日 15:56:57   作者:springsnow  
這篇文章介紹了C#中的SQLCommand命令與DbTransaction事務(wù)處理,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、SqlDataReader

SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pwd=IT@SME2018;database=MESOtheata;");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from [A_Emp_Dept_20190522]", conn);//    或者
                                                                             //或者    SqlCommand cmd=conn.CreateCommand();cmd.CommandText="";
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);//讀取完畢自動(dòng)關(guān)閉連接
for (int i = 0; i < reader.FieldCount - 1; i++)
{
    Console.WriteLine(reader.GetName(i));//獲取字段名
    while (reader.Read())//或者使用reader.HasRows屬性判斷時(shí)候有記錄
    {
        Console.WriteLine(reader.GetDateTime(0).ToString());
        //或者 Console.WriteLine(Convert.ToDateTime(reader[0]).ToString());
        object[] values = new object[reader.FieldCount - 1];
        reader.GetValues(values);//讀取一行所有字段的內(nèi)容到Values字段中
        if (reader.IsDBNull(0)
        {
            //判斷某字段是否為null值。
        }
    }

}
reader.Close();
//系統(tǒng)未關(guān)閉時(shí),本Command更改CommandText后或新建Command還有繼續(xù)使用此連接。
//conn.Close();

二、參數(shù)化查詢

1、使用SQLClient:

SqlCommand cmd = new SqlCommand("select * from [A_Emp_Dept_20190522] where  empname=@empname", conn);
SqlParameter para = new SqlParameter("@empname", SqlDbType.NVarChar, 10);
para.Value = "曾亮";
cmd.Parameters.Add(para);
//或者直接 cmd.Parameters.AddWithValue("@empname","曾亮");

2、使用OleDb:

OleDbCommand cmd = new OleDbCommand("select * from [A_Emp_Dept_20190522] where  empname like ?  and line like ?", conn);
cmd.Parameters.AddWithValue("@empname", "曾亮");
cmd.Parameters.AddWithValue("@line", "ODMZ%");//參加的參數(shù)的順序應(yīng)與?號(hào)的順序相同,參數(shù)名可隨意執(zhí)行,甚至可以是空串。

三、執(zhí)行無(wú)返回值的查詢:cmd.ExecuteNonQuery()

SqlCommand cmd = new SqlCommand("update  [A_Emp_Dept_20190522] set dept='aa' where  dept='' ", conn);
int RecordAffected = cmd.ExecuteNonQuery();
if (RecordAffected == 0)
{
    Console.Write("操作失敗");
}

四、執(zhí)行返回單值的查詢:cmd.ExecuteScalar()

SqlCommand cmd = new SqlCommand("select count(*) from   [A_Emp_Dept_20190522] ", conn);
if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)//無(wú)記錄,返回null
{
    Console.Write("無(wú)記錄");
}

五、使用Transaction事務(wù)

SqlTransaction trans = null;
try
{
    trans = conn.BeginTransaction();//默認(rèn)事務(wù)隔離級(jí)別為ISolationLevel.Serilizable,Oracle為ISolationLevel.ReadCommited
    SqlCommand cmd = new SqlCommand("Insert int **", conn);
    cmd.Transaction = trans;//為命令指定一個(gè)事務(wù)
    cmd.ExecuteNonQuery();
    //可以有多個(gè)SqlCommand一并執(zhí)行,一起提交或回滾
    trans.Commit();
}
catch
{
    trans.Rollback();
}

Transaction事務(wù)隔離級(jí)別:

六、調(diào)用存儲(chǔ)過程

SqlCommand cmd = new SqlCommand("GetCustomer", conn);
cmd.CommandType = CommandType.StoredProcedure;

SqlParameter para1 = new SqlParameter("@CustomerID", SqlDbType.NChar, 10);
para1.Value = "UINET";
cmd.Parameters.Add(para1);

SqlParameter para2 = new SqlParameter("@CompanyName", SqlDbType.NVarChar, 30);
para2.Direction = ParameterDirection.Output;
cmd.Parameters.Add(para2);

cmd.ExecuteNonQuery();
Console.WriteLine(cmd.Parameters["@CompanyName"].Value);

七、使用DataReader獲取數(shù)據(jù)庫(kù)模式信息

SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly);//僅僅讀取模式信息
DataTable table = reader.GetSchemaTable();
foreach (DataRow row in table.Rows)
{
    foreach (DataColumn col in table.Columns)
    {
        Console.WriteLine(col.ColumnName + row[col.ColumnName]);
    }
}

八、瀏覽多個(gè)結(jié)果集

SqlCommand cmd = new SqlCommand("select * from Catogories;select * from Customers", conn);

SqlDataReader reader = cmd.ExecuteReader();//僅僅讀取模式信息
DataTable table = reader.GetSchemaTable();
do//首先自動(dòng)定位在第一個(gè)結(jié)果集上
{
    while (reader.Read())
    {
        Console.Write(reader[0]);
    }
} while (reader.NextResult());

九、DbDataReader類

從數(shù)據(jù)源中讀取行的只進(jìn)流,包含SqlDataReader ,OleDbDataReader ,OdbcDataReader ,實(shí)現(xiàn)了IDataReader IDataRecord 接口

  • 屬性
    IsClosed    獲取一個(gè)值,該值指示數(shù)據(jù)讀取器是否已關(guān)閉。
    FieldCount    獲取當(dāng)前行中的列數(shù)。
    Item[Int32]    獲取位于指定索引處的列。
    Item[String]    獲取具有指定名稱的列。
  • 方法
    Close()    關(guān)閉 IDataReader 對(duì)象。
    GetSchemaTable()    返回一個(gè) DataTable,它描述 IDataReader 的列元數(shù)據(jù)。
    NextResult()    在讀取一批 SQL 語(yǔ)句的結(jié)果時(shí),使數(shù)據(jù)讀取器前進(jìn)到下一個(gè)結(jié)果。
    Read()    讓 IDataReader 前進(jìn)到下一條記錄。
    Get***(Int32)    獲取指定列的值。
    GetValues(Object[])    使用當(dāng)前記錄的列值填充對(duì)象的數(shù)組。
    IsDBNull(Int32)    返回指定字段是否設(shè)置為 null。

到此這篇關(guān)于C#SQLCommand命令與DbTransaction事務(wù)處理的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論