ExecuteNonQuery(),ExecuteScalar(),ExecuteReader的相关用法

1、 增加新的记录 
private void Page_Load(object sender, System.EventArgs e) 

MyConnection.Open();’打开数据库 
MyCommand1.CommandText = "insert into admin values(‘aaddq‘,‘as‘,‘ss‘)"; 
MyCommand1.Connection = MyConnection; 
MyCommand1.ExecuteNonQuery();’由于增加了一条记录,所以返回1 
//或者MyCommand1.ExecuteReader();先增加一条记录,然后返回一个System.Data.OleDb.OleDbDataReader类型的对象,该对象为:EOF 
//或者MyCommand1. ExecuteScalar();先增加一条记录,返回未实列化的对象 
MyConnection.Close(); 

 

2、 删除现有数据 
private void Page_Load(object sender, System.EventArgs e) 

MyConnection.Open();’打开数据库 
MyCommand1.CommandText = "delete * from admin"; 
MyCommand1.Connection = MyConnection; 
MyCommand1.ExecuteNonQuery();’由于删除了n条记录,所以返回n 
//或者MyCommand1.ExecuteReader();先删除n条记录,然后返回一个System.Data.OleDb.OleDbDataReader类型的对象,该对象为:EOF 
//或者MyCommand1. ExecuteScalar();先删除n条记录,返回未实列化的对象 
MyConnection.Close(); 

 

3、 修改现有数据 
private void Page_Load(object sender, System.EventArgs e) 

MyConnection.Open();’打开数据库 
MyCommand1.CommandText = "update admin set admin_code=’212’,Admin_pwd=’43’ where admin_code=’23’"; 
MyCommand1.Connection = MyConnection; 
MyCommand1.ExecuteNonQuery();’由于修改了1条记录,所以返回n 
//或者MyCommand1.ExecuteReader();先修改了1条记录,然后返回一个System.Data.OleDb.OleDbDataReader类型的对象,该对象为:EOF 
//或者MyCommand1. ExecuteScalar();先修改了1条记录,返回未实列化的对象 
MyConnection.Close(); 

 

关于MyCommand的ExecuteNonQuery(),ExecuteScalar(),ExecuteReader方法的区别: 
1、ExecuteNonQuery():执行SQL,返回一个整型变量,如果SQL是对数据库的记录进行操作,那么返回操作影响的记录条数,如果是SQL="CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)"那么在表创建成功后该方法返回 -1。 
例如: 
private void Page_Load(object sender, System.EventArgs e) 

MyConnection.Open();’打开数据库 
MyCommand1.CommandText = "CREATE TABLE LookupCodes (code_id smallint IDENTITY(1,1) PRIMARY KEY CLUSTERED, code_desc varchar(50) NOT NULL)"; MyCommand1.Connection = MyConnection; 
MyCommand1.ExecuteNonQuery();’首先建立一个LookupCodes表,然后返回-1 
//或者MyCommand1.ExecuteReader();首先建立一个LookupCodes表,然后返回一个System.Data.OleDb.OleDbDataReader类型的对象,该对象为:EOF 
//或者MyCommand1. ExecuteScalar();首先建立一个LookupCodes表,返回未实列化的对象 
MyConnection.Close(); 



2、 ExecuteScalar():执行SQL,(如果SQL是查询Select)返回查询结果的第一行第一列,如果(如果SQL不是查询Select)那么返回未实列化的对象,因为对象未实列化,所以返回结果不能ToString(),不能Equals(null),也就是说返回结果没有任何作用 

3、 executeReader方法执行SQL,(如果SQL是查询Select)返回查询结果的集合,类型是System.Data.OleDb.OleDbDataReader,你可以通过此结果,获取查询的数据。如果(如果SQL不是查询Select)那么返回一个没有任何数据的System.Data.OleDb.OleDbDataReader类型的集合(EOF) 

 

实际例子:

     参数接口:

     

//获取用户信息
[HttpGet]
public Models.Return Getuserinfo()
{
Models.Return ret = new Models.Return();
HttpRequest request = HttpContext.Current.Request;
string openid = request["openid"];
if (string.IsNullOrEmpty(openid))
{
ret.code = 119;
ret.msg = "参数不全";
}
else
{
try
{
string sql = "";
List<DbParameter> ps = new List<DbParameter>();
ndb = Tools.Db;
ndb.Open();
sql = "select wx_openid,id,realname,user_name,mobile,idnum,money,score from eg_user where wx_openid=@openid";
ps.Add(ndb.CreateParameter("@openid", openid));
DataTable dt = ndb.ExecuteDataTable(sql, ps);
ArrayList data = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
data.Add(new
{
id = dr["id"],
realname = dr["realname"],
user_name = dr["user_name"],
mobile = dr["mobile"],
idnum = dr["idnum"],
money = dr["money"],
score = dr["score"]
});
}
ret.code = 0;
ret.data = data;
}
catch (Exception ex)
{
Tools.Log.Error("Error", ex);
ret.code = 100;
}
finally
{
if (ndb != null)
{
ndb.Close();
ndb = null;
}
}
}
return ret;
}

 

 

[HttpPost]
public Models.Return Recharge()
{
Models.Return ret = new Models.Return();
HttpRequest request = HttpContext.Current.Request;
string openid = request["openid"];
string re_money = request["Re_money"];
string re_score = request["Re_score"];
if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(re_money) || string.IsNullOrEmpty(re_score))
{
ret.code = 119;
ret.msg = "参数输入不完整";
}
else
{
try
{
string sql = "";
List<DbParameter> ps = new List<DbParameter>();
ndb = Tools.Db;
ndb.Open();
sql = "select id from eg_user where wx_openid=@openid";
ps.Add(ndb.CreateParameter("@openid", openid));
int id = Convert.ToInt32(ndb.ExecuteScalar(sql, ps));//获取eg_user 表中的id,ExecuteScalar(如果SQL是查询Select)返回查询结果的第一行第一列
sql = "update eg_user set money=money+@money,score=score+@score,total_score+total_score+@score where id=@id";
ps.Add(ndb.CreateParameter("@id", id));
ps.Add(ndb.CreateParameter("@money", double.Parse(re_money)));
ps.Add(ndb.CreateParameter("@score", double.Parse(re_score)));
ndb.ExecuteNonQuery(sql, ps);

sql = "insert into eg_user_log(log_info,log_time,log_admin_id,log_user_id,money,score,point,user_id) values('充值缴费',@log_time,@log_admin_id,@log_user_id,@money,@score,@point,@user_id)";
ps.Clear();
ps.Add(ndb.CreateParameter("@log_time", Library.Net.Common.Time.ConvertDateTimeInt(DateTime.Now)));//将时间转换为时间戳格式
ps.Add(ndb.CreateParameter("@log_admin_id", Tools.SessionSupplier == null ? 0 : Tools.SessionSupplier.id));//三目运算,减少错误
ps.Add(ndb.CreateParameter("@log_user_id", 0));
ps.Add(ndb.CreateParameter("@money", re_money));
ps.Add(ndb.CreateParameter("@score", re_score));
ps.Add(ndb.CreateParameter("@point", 0));
ps.Add(ndb.CreateParameter("@user_id", id));
ndb.ExecuteNonQuery(sql, ps);

sql = "select wx_openid,id,realname,user_name,mobile,idnum,money,score from eg_user where wx_openid=@openid_sel";
ps.Add(ndb.CreateParameter("@openid_sel", openid));
DataTable dt = ndb.ExecuteDataTable(sql, ps);
ArrayList data = new ArrayList();
foreach (DataRow dr in dt.Rows)
{
data.Add(new
{
id = dr["id"],
realname = dr["realname"],
user_name = dr["user_name"],
mobile = dr["mobile"],
idnum = dr["idnum"],
money = dr["money"],
score = dr["score"]
});
}
if (data.Count > 0)
{
ret.code = 0;
ret.msg = "充值成功";
ret.data = data;
}
else
{
ret.code = 1;
ret.msg = "充值失败";
ret.data = null;
}

}
catch (Exception ex)
{
Tools.Log.Error("Error", ex);
ret.code = 110;
}
finally
{
if (ndb != null)
{
ndb.Close();
ndb = null;
}
}
}
return ret;
}

你可能感兴趣的:(ExecuteNonQuery(),ExecuteScalar(),ExecuteReader的相关用法)