SqlConnection sqlconn = new SqlConnection(conn);
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand();
sqlcomm.Connection = sqlconn;
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.CommandText = "proc_execchuku";
SqlParameter[] pa ={
new SqlParameter("@productbianhao",SqlDbType.Int),
new SqlParameter("@productname",SqlDbType.NVarChar,100),
new SqlParameter("@outcount",SqlDbType.Int),
new SqlParameter("@updatetime",SqlDbType.DateTime)
};
for (int i = 0; i < pa.Length; i++)
{
sqlcomm.Parameters.Add(pa[i]);
}
pa[0].Value = productbianhao;
pa[1].Value = productname;
pa[2].Value = output;
pa[3].Value = updatetime;
int flag = sqlcomm.ExecuteNonQuery();
sqlconn.Close();
sqlconn.Dispose();
return flag;执行时此处返回的结果为2,
但是数据库记录没变化
sqlconn.Open();
SqlCommand sqlcomm = new SqlCommand();
sqlcomm.Connection = sqlconn;
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.CommandText = "proc_execchuku";
SqlParameter[] pa ={
new SqlParameter("@productbianhao",SqlDbType.Int),
new SqlParameter("@productname",SqlDbType.NVarChar,100),
new SqlParameter("@outcount",SqlDbType.Int),
new SqlParameter("@updatetime",SqlDbType.DateTime)
};
for (int i = 0; i < pa.Length; i++)
{
sqlcomm.Parameters.Add(pa[i]);
}
pa[0].Value = productbianhao;
pa[1].Value = productname;
pa[2].Value = output;
pa[3].Value = updatetime;
int flag = sqlcomm.ExecuteNonQuery();
sqlconn.Close();
sqlconn.Dispose();
return flag;执行时此处返回的结果为2,
但是数据库记录没变化
如果不是单纯的update,这里提示的返回结果2实际上是不准确的,并不代表Update了2笔资料2.确定Update前后的值有不同吗?
@productbianhao int,
@productname nvarchar(100),
@outcount int,
@updatetime datetime
as
declare @productid int
begin
begin tran
update biao1 set count=count-@count where bianhao=@productbianhao and productname=@productname
select productid=id from biao1 where bianhao=@productbianhao and productname=@productname
insert into biao2 (productbianhao,productname,count,updatetime,productid) values
(@productbianhao,@productname,@outcount,@@updatetime,@productid )
if(@@error<>0)
begin
commit tran
end
else
begin
rollback tran
end
end
go不好意思,大概的存储过程就是这样的了
@productbianhao int,
@productname nvarchar(100),
@outcount int,
@updatetime datetime
as
declare @productid int
begin
begin tran
update biao1 set count=count-@count where bianhao=@productbianhao and productname=@productname
select productid=id from biao1 where bianhao=@productbianhao and productname=@productname
insert into biao2 (productbianhao,productname,count,updatetime,productid) values
(@productbianhao,@productname,@outcount,@@updatetime,@productid )
if(@@error<>0)
begin
rollback tran
end
else
begin
commit tran
end
go
{
sqlcomm.Parameters.Add(pa[i]);
}
pa[0].Value = productbianhao;
pa[1].Value = productname;
pa[2].Value = output;
pa[3].Value = updatetime;
我以为这一句有问题,你的参数应该先赋值然后再添加进去,因此只需颠倒一下就行
pa[0].Value = productbianhao;
pa[1].Value = productname;
pa[2].Value = output;
pa[3].Value = updatetime;
for (int i = 0; i < pa.Length; i++)
{
sqlcomm.Parameters.Add(pa[i]);
}
因为没有先赋值,系统就用默认的值代替了,如果你的数据库里正好有两条记录中某项值为默认值的话空值记录的话,那返回的当然是2了,