这可能是不可能的,但是我有一个接受' list'的方法,这个方法将通过循环'列表来执行多个sql server存储过程。对象
public void ExecuteMultipleProc(string strcon, ref List<KeyValuePair<string, SqlParameter[]>> list)
{
using (SqlConnection sqlcon = new SqlConnection(strcon)) {
sqlcon.Open();
SqlTransaction sqltran = default(SqlTransaction);
sqltran = sqlcon.BeginTransaction();
try {
foreach (KeyValuePair<string, SqlParameter[]> kv in list) {
SqlCommand sqlcmd = new SqlCommand(kv.Key, sqlcon);
sqlcmd.CommandType = CommandType.StoredProcedure;
if ((kv.Value == null) == false) {
foreach (SqlParameter x in kv.Value) {
sqlcmd.Parameters.Add(x);
}
}
sqlcmd.Transaction = sqltran;
sqlcmd.ExecuteNonQuery();
}
sqltran.Commit();
} catch (Exception ex) {
sqltran.Rollback();
throw;
}
}
}
当我需要插入关系称为Header Detail表的表时,我遇到了问题,因为我的InsertDetail需要从InsertHeader输出,例如:
List<KeyValuePair<string, SqlParameter[]>> list = new List<KeyValuePair<string, SqlParameter[]>>();
SqlParameter[] params = new SqlParameter[2];
params(0) = new SqlParameter("@test", SqlDbType.VarChar, 200);
params(0).Value = "TEST";
params(1) = new SqlParameter("@id", SqlDbType.Int);
params(1).Direction = ParameterDirection.Output;
list.Add(new KeyValuePair<string, SqlParameter[]>("spInsertHeader", params));
SqlParameter[] param = new SqlParameter[1];
param(0) = new SqlParameter("@id", SqlDbType.Int);
param(0).Value = Convert.ToInt32(params(1).Value); //THIS LINE WILL NEVER HAVE A VALUE BECAUSE THE CALL TO DATABASE ISN'T HAPPENED YET
list.Add(new KeyValuePair<string, SqlParameter[]>("spInsertDetail", param));
ExecuteMultipleProc(GetConnectionString(), list);
我的问题:当我的方法调用InsertHeader并将输出值传递给InsertDetail而不破坏方法功能时,可以通过检测值更改来实现此目的。对不起英语不好,任何帮助将不胜感激。