代码
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]);
}
}
}