jayleke

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
代码
string sql = "delete from Users where ID=3";
SqlConnection conn
= new SqlConnection(sqlConnString);
SqlCommand cmd
= new SqlCommand(sql, conn);
try
{
conn.Open();
sqldataReader dr
=cmd.ExecutReader();

while(dr.Reader())

{

responser.write(dr[
"Name"]);

}

}
catch (SqlException sqlex)
{
Response.Write(sqlex.Message
+ "<br>");
}
finally
{
conn.Close();
}

2、DataAdapter和DataSet、DataView读取单列数据

string sql = "select * from Users";
SqlConnection conn
= new SqlConnection(sqlConnString);
SqlDataAdapter da
= new SqlDataAdapter(sql, conn);
try
{
conn.Open();
DataSet ds
= new DataSet("Users");
da.Fill(ds,
"Users");
DataView dv
= ds.Tables[0].DefaultView;

//DataView读取单列
for (int i = 0; i < dv.Table.Rows.Count; i++)
{
Response.Write(dv.Table.Rows[i][
"ID"].ToString()+" "+"<br>");
}

//DataSet读取单列
foreach (DataRow row in ds.Tables[0].Rows)
Response.Write(row[
"Name"].ToString()+" "+row[0].ToString()+"<br>");
DataList1.DataSource
= dv;
DataList1.DataBind();
GridView1.DataSource
= ds;
GridView1.DataBind();
}
catch (SqlException exception)
{
Response.Write(exception.Message
+ "<br>");
}
finally
{
conn.Close();
}

 

 

循环读取dataset

代码
DataSet ds = new DataSet();

foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
foreach (DataColumn dc in dt.Columns)
{
System.Console.Write(dr[dc]);
}
}
}

 

posted on 2010-08-02 22:55  jayleke  阅读(2516)  评论(0编辑  收藏  举报
葳蕤工作室