如果您請幫我解決問題,我正嘗試使用所有4個鍵(Role_ID, Track_ID, Person_ID, Conference_ID
)上的唯一約束來更新表。具有唯一約束條件的SQL更新命令asp.net
爲什麼我不能更新它?
錯誤:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Violation of UNIQUE KEY constraint 'UK_conferenceRole'. Cannot insert duplicate key in object 'dbo.ConferenceRole'. The statement has been terminated.
代碼:
var sqlCon7 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string query7 = @"update ConferenceRole set Role_ID =" + 3 + ",Person_ID='" + idp + "'where Conference_ID ='" + conference + "'and Track_ID='" + TrackId + "'";
SqlCommand cmd7 = new SqlCommand(query7, sqlCon7);
cmd7.CommandType = CommandType.Text;
try
{
sqlCon7.Open();
cmd7.ExecuteNonQuery();
sqlCon7.Close();
}
catch (Exception ee)
{
throw ee;
}
在此先感謝
首先,請停止直接使用ADO.NET ,至少使用像這樣的庫http://abstractsql.codeplex.com – BlackTigerX
請,請停止使用字符串連接來建立SQL查詢!你已經使用了一個SqlCommand對象,所以也使用參數! –