查询返回单行ID

string sql = string.Format("select id from account where name='{0}' and password='{1}'", name, password);
object AID = SqlHelper.ExecuteScalar(CON_STR, CommandType.Text, sql);
int ID = Convert.ToInt32(AID);


插入返回新记录ID
string sql = string.Format("insert into account (name ,password) values ('{0}', '{1}'); Select CAST(@@Identity AS INTEGER)", account.Name, account.Password);
object AID = SqlHelper.ExecuteScalar(CON_STR, CommandType.Text, sql );
int id = Convert.ToInt32(AID);


查询返回多行记录

string sql = string.Format("select id, name, password from account where id={0}", id);
SqlDataReader dr 
= SqlHelper.ExecuteReader(CON_STR, CommandType.Text, sql);
if (dr.Read()) 
{
    acc 
=  new Account(dr.GetInt32(0), dr.GetString(1), dr.GetString(2));
}

dr.Close();



应该用SqlParameter来构造,并且DAAB带有Cache,可以提高效率.

相关文章:

  • 2022-03-03
  • 2021-06-26
  • 2021-11-20
  • 2021-12-31
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-13
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案