當我運行這段代碼我就趕上(例外五)部分得到了一個錯誤,我不知道爲什麼,編譯器說的「當地命名爲「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();
}
希望你的回覆!謝謝!
請注意,如果您在MSDN中查找錯誤代碼(如CS0136在你的情況下),你會得到的文章,解釋常見的情況,並展示樣品 - [編譯器錯誤CS0136(http://msdn.microsoft.com/en-us /library/973aa6bt%28v=vs.90%29.aspx) –