当我运行此代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说“不能在此范围内声明名为 'e' 的局部变量,因为它会给 ' 赋予不同的含义e',已在“父级或当前”范围中用于表示其他内容”
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();
}
希望得到您的回复!谢谢!