如何将返回的memberID
变量插回到其他SQL存储过程中?
protected void imgBtnActivate_onClick(object sender, EventArgs e)
{
// Inserts Member record
try
{
Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]);
DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p");
db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text);
db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text);
db.ExecuteNonQuery(dbCommand);
int memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString());
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Notify Policy");
}
// Insert Member Email
try
{
Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]);
DbCommand dbCommand = db.GetStoredProcCommand("[ins_MemberEmail_p]");
dbCommand.Transaction = dbTran;
db.AddInParameter(dbCommand, "MemberID", DbType.Int32, memberID);
db.AddInParameter(dbCommand, "SourceID", DBType.In32, 33);
db.AddInParameter(dbCommand, "EmailAddress", DbType.String, EncryptedEmailAddress);
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Notify Policy");
}
}
答案 0 :(得分:3)
只需在第一个int memberID
块之外定义try....catch
,以便它在第二个try...catch
块中可见:
protected void imgBtnActivate_onClick(object sender, EventArgs e)
{
// define it here
int memberID = -1:
// Inserts Member record
try
{
......
db.ExecuteNonQuery(dbCommand);
memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString());
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Notify Policy");
}
// Insert Member Email
try
{
......
// then it'll be visible and useable here!
db.AddInParameter(dbCommand, "MemberID", DbType.Int32, memberID);
.....
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Notify Policy");
}
}
答案 1 :(得分:1)
像这样改变它,将变量的声明移出{}之前的<}。
protected void imgBtnActivate_onClick(object sender, EventArgs e)
{
int memberID;
// Inserts Member record
try
{
Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]);
DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p");
db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text);
db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text);
db.ExecuteNonQuery(dbCommand);
memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString());
}
catch (Exception ex)
{
ExceptionPolicy.HandleException(ex, "Notify Policy");
}
答案 2 :(得分:0)
首先,我建议您稍微分解一下代码。您正在控制事件处理程序中执行相当多的逻辑(包括数据访问和可能的某些域逻辑)。
但问题的直接解决方法是在memberID
阻止之外删除try/catch
。
protected void imgBtnActivate_onClick(object sender, EventArgs e)
{
int memberID;
// Inserts Member record
try
{
Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings["ConnectionString.Dev"]);
DbCommand dbCommand = db.GetStoredProcCommand("dbo.ins_member_p");
db.AddInParameter(dbCommand, "@FirstName", DbType.String, txtFirstName.Text);
db.AddInParameter(dbCommand, "@LastName", DbType.String, txtLastName.Text);
db.ExecuteNonQuery(dbCommand);
memberID = Convert.ToInt32(db.GetParameterValue(dbCommand, "MemberNewID").ToString());
}
// .... so on and so forth
上面的方法是在整个事件处理程序的范围内有memberID
,这反过来允许你在处理程序的整个范围内使用该变量。