【发布时间】:2013-08-01 01:56:29
【问题描述】:
当我运行此代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说 “不能在此范围内声明名为 'e' 的局部变量,因为它会给 'e' 赋予不同的含义,它已经在 'parent or current' 范围内用于表示其他东西"
try
{
//Form Query which will insert Company and will output generated id
myCommand.CommandText = "Insert into Comp(company_name) Output Inserted.ID VALUES (@company_name)";
myCommand.Parameters.AddWithValue("@company_name", txtCompName);
int companyId = Convert.ToInt32(myCommand.ExecuteScalar());
//For the next scenario, in case you need to execute another command do it before committing the transaction
myTrans.Commit();
//Output Message in message box
MessageBox.Show("Added", "Company Added with id" + companyId, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
MessageBox.Show("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}
MessageBox.Show("An exception of type " + e.GetType() +
"was encountered while inserting the data.");
MessageBox.Show("Record was written to database.");
}
finally
{
myConnection.Close();
}
希望得到您的回复!谢谢!
【问题讨论】:
-
请注意,如果您在 MSDN 中搜索错误代码(如您的案例中的 CS0136),您将获得解释常见案例并展示示例的文章 - Compiler Error CS0136