您的位置:首页 > 其它

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

2008-03-31 10:37 375 查看
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())

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();

设置一个NULL 值 使用 DBNull.Value

返回 ojbect[]

new object[] {...}

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

添加行 LoadDataRow
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: