比如就这个方法而言 public static int UpdateProductInfo(Products pro)
{
string sql = "update ProductInfo set productName=@name,productPrice=@price,categoryId=@categoryId,productinventory=@inventory,productImages=@images,productDescription=@description,productVolume=@volume,departId=@departId where productId="+pro.ProductId;
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@name",pro.ProductName),
new SqlParameter("@price",pro.ProductPrice),
new SqlParameter("@categoryId",pro.CategoryId.CategoryId),
new SqlParameter("@inventory",pro.Productinventory) ,
new SqlParameter("@images",pro.ProductImages),
new SqlParameter("@description",pro.ProductDescription),
new SqlParameter("@volume",pro.ProductVolume),
new SqlParameter("@departId",pro.DepartId.DepartId)
//new SqlParameter("@proId",pro.ProductId)
};
int count = DBHelpers.ExecuteCommand(sql,para);
return count;
}
给sql语句如何加事务呢?? 帮忙解决~ 感激。
{
string sql = "update ProductInfo set productName=@name,productPrice=@price,categoryId=@categoryId,productinventory=@inventory,productImages=@images,productDescription=@description,productVolume=@volume,departId=@departId where productId="+pro.ProductId;
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("@name",pro.ProductName),
new SqlParameter("@price",pro.ProductPrice),
new SqlParameter("@categoryId",pro.CategoryId.CategoryId),
new SqlParameter("@inventory",pro.Productinventory) ,
new SqlParameter("@images",pro.ProductImages),
new SqlParameter("@description",pro.ProductDescription),
new SqlParameter("@volume",pro.ProductVolume),
new SqlParameter("@departId",pro.DepartId.DepartId)
//new SqlParameter("@proId",pro.ProductId)
};
int count = DBHelpers.ExecuteCommand(sql,para);
return count;
}
给sql语句如何加事务呢?? 帮忙解决~ 感激。
try
{
c.ExecuteNonQuery();
}
catch (Exception)
{
c.Transaction.Rollback();
}这样试试!
下面的代码中,我们想执行两相查询语句,分别往TABLE1和TABLE2各插入一条数据,如果任何其中一个操作失败,那我们就撤销。要么两个表都插入数据,要么就一个表也别插入。 using ( SqlConnection conn = new SqlConnection( connectionString ) )
{
conn.Open();
SqlTransaction transaction = conn.BeginTransaction();
SqlCommand cmd1 = conn.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = INSERT [TABLE1] ( [ID], [Value] VALUES ( @ID, @Value );
cmd1.Parameters.Add( new SqlParameter( @ID, 1 ) );
cmd1.Parameters.Add( new SqlParameter( @Value, sdfasdf ) );
SqlCommand cmd2 = conn.CreateCommand();
cmd2.CommandType = CommandType.Text;
cmd2.CommandText = INSERT [TABLE2] ( [ID], [Value] VALUES ( @ID, @Value );
cmd2.Parameters.Add( new SqlParameter( @ID, 2 ) );
cmd2.Parameters.Add( new SqlParameter( @Value, xxx ) );
try
{
cmd1.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
如果执行到这里,还没出错,那说明两个操作都成功了,我们就提交事务,让这两个查询生效。
transaction.Commit();
}
catch
{
如果执行到这里,就说明肯定有操作失败了,那么我们就回滚操作,回滚的范围就是自多调用BeginTransaction之后在该数据库连接上执行的所有事务操作。
transaction.Rollback();
}
}
当然,也可以在BeginTransaction上使用参数,,比如事务也可以按名称保存还原点等,这些在T-SQL参考中或者SqlServer的帮助文档中可以找到。
--这样不对吗
ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
报上面的那个错误。。