ADO.NET 2.0技术内幕-读书笔记

using
在using代码块内所有可能的位置创建短期生存对象

使用NextResult()取多个结果集
SqlConnection con  =   new  SqlConnection( " data source=(local);initial catalog=ThreeLayer;persist security info=False;user id=sa;password=; " );
        con.Open();
        SqlCommand cmd 
=  con.CreateCommand();
        cmd.CommandText 
=   " select * from Limit; select * from Message " ;
        SqlDataReader dr 
=  cmd.ExecuteReader();

        
// 使用NextResult()取多个结果集
        
// do
        
// {
        
//     while (dr.Read())
        
//     {
        
//         Console.Write("{0}-{1}",dr[0],dr[1]);
        
//         Console.WriteLine();
        
//     }
        
// } while (dr.NextResult());

        
// Read())方法 取一个
         while  (dr.Read())
        
{
            Console.Write(
"{0}-{1}", dr[0], dr[1]);
            Console.WriteLine();
        }

        dr.Close();
        con.Close();

参数化查询

string  strConn, strSQL;

strConn 
=   @" Data Source=.\SQLExpress; "   +

             
" Initial Catalog=Northwind;Integrated Security=True; " ;

strSQL 
=   " SELECT @UnitPrice = UnitPrice, @UnitsInStock = UnitsInStock  "   +

          
" FROM Products WHERE ProductName = @ProductName " ;

SqlConnection cn 
=   new  SqlConnection(strConn);

cn.Open();

SqlCommand cmd 
=   new  SqlCommand(strSQL, cn);

SqlParameter pUnitPrice, pInStock, pProductName;

pUnitPrice 
=  cmd.Parameters.Add( " @UnitPrice " , SqlDbType.Money);

pUnitPrice.Direction 
=  ParameterDirection.Output;

pInStock 
=  cmd.Parameters.Add( " @UnitsInStock " , SqlDbType.NVarChar,  20 );

pInStock.Direction 
=  ParameterDirection.Output;

pProductName 
=  cmd.Parameters.Add( " @ProductName " , SqlDbType.NVarChar,  40 );

pProductName.Value 
=   " Chai " ;

cmd.ExecuteNonQuery();

if  (pUnitPrice.Value  ==  DBNull.Value)  {

     Console.WriteLine(
"No product found named {0}", pProductName.Value);

}
  else   {

    Console.WriteLine(
"Unit Price: {0}", pUnitPrice.Value);

    Console.WriteLine(
"In Stock: {0}", pInStock.Value);

}





设置一个NULL 值 使用 DBNull.Value

返回 ojbect[] 
new object[] {...}

DataTable[] tables = new DataTable() {ds.Table[1],ds.Table[1]}

添加行 LoadDataRow

你可能感兴趣的:(.net)