我正在尝试在主表和详细信息表中输入数据。
我的主桌Qry:Insert INTO tblMain();
然后我在做:
declare @OrderID as int;
set @OrderID=scope_identity();
Update Orders SET OrderID=(LTRIM(RTRIM(OrderID))+ RIGHT('0000000'+CAST(@OrderID AS VARCHAR(7)),7)) WHERE OrderId=@OrderID;
SELECT @OrderID
然后我使用@OrderID
的值插入Detail表中
Insert into tbl_Details(OrderID,SKU,amount) Values(@OrderID,NULL,20 );
如果在tbl_Details
中,SKU
不为空,如果我立即在ExecuteScalar
中运行所有查询并将SKU
传递为null,则不会出现异常。
我的执行功能是:
public static double ExecuteScalarQuery(string SQL, ref string ErrMsg)
{
ErrMsg = "";
using (SqlConnection conn = new SqlConnection(GetDBConn()))
{
SqlTransaction otrans = null;
double dblRetVa = 0;
try
{
conn.Open();
otrans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand(SQL, conn);
cmd.Transaction = otrans;
dblRetVa = double.Parse(cmd.ExecuteScalar().ToString());
otrans.Commit();
conn.Close();
}
catch (Exception ex)
{
ErrMsg = ex.ToString().Trim();
if (otrans != null) otrans.Rollback();
}
return dblRetVa;
}
}
我有解决方案,但为此我必须更改我的功能的签名如下:
public static double ExecuteNonQueryWithSelect(string SQL, string sQry2,string SelectColID , ref string ErrMsg)
{
ErrMsg = "";
using (SqlConnection conn = new SqlConnection(GetDBConn()))
{
SqlTransaction otrans = null;
double dblRetVal = 0;
try
{
conn.Open();
otrans = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand(SQL, conn);
cmd.Transaction = otrans;
dblRetVal = double.Parse(cmd.ExecuteScalar().ToString());
SqlCommand cmd2 = new SqlCommand(sQry2.Replace(SelectColID.Trim(), dblRetVal.ToString()), conn);
cmd2.Transaction = otrans;
cmd2.ExecuteNonQuery();
otrans.Commit();
conn.Close();
}
catch (Exception ex)
{
ErrMsg = ex.ToString().Trim();
if (otrans != null) { otrans.Rollback(); dblRetVal = 0; }
}
return dblRetVal;
}
}
我想要的是我是否可以在我的旧函数中执行ant事务,以便在详细信息表中的某些不可为空的字段作为null传递而不更改我的函数的签名时抛出异常。
答案 0 :(得分:0)