//得到人员名称
public string GetStaff(string StaffID)
{ string Stype = "";
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
if (StaffID != "")
{
SqlCommand scd = new SqlCommand("select id,usrid from DB_Admin where id=" + StaffID, strcon);
SqlDataReader dr = scd.ExecuteReader();
if (dr.Read())
{
Stype = dr[1].ToString();
return Stype; }
dr.Close();
}
return Stype;
strcon.Close(); }上面是有datereader写的,如何把上面的函数改成用DataAdapter写的
public string GetStaff(string StaffID)
{ string Stype = "";
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
if (StaffID != "")
{
SqlCommand scd = new SqlCommand("select id,usrid from DB_Admin where id=" + StaffID, strcon);
SqlDataReader dr = scd.ExecuteReader();
if (dr.Read())
{
Stype = dr[1].ToString();
return Stype; }
dr.Close();
}
return Stype;
strcon.Close(); }上面是有datereader写的,如何把上面的函数改成用DataAdapter写的
{ string Stype = "";
DataTable dtl = new DataTable();
SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
strcon.Open();
if (StaffID != "")
{
SqlCommand scd = new SqlCommand("select id,usrid from DB_Admin where id=" + StaffID, strcon);
SqlDataAdapter da = new SqlDataAdapter(scd);
da.Fill(dtl); }
strcon.Close();
return dtl;
}
DataSet ds = new DataSet();
da.Fill(ds, "name");
da.Fill(dtl);
另外,如果只求一笔记录第一个字段,sqlCommand有一个方法,就是ExecuteScalar(),也不必用到SqlDataAdapter