我有一个简单的按钮ADD
ADD_Click代码是:
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
我想要的是,如果有人添加具有相同HESAP
(主键)的数据,我想要一个带有这个词的alert()函数。 “已经有相同的数据”或任何文字。无所谓。
我该怎么做?
注意:我希望获得相同的HESAP值并发现错误。
答案 0 :(得分:5)
试试这个:
protected void Add_Click(object sender, EventArgs e)
{
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
using (SqlConnection myConnection = new SqlConnection(strConnectionString))
{
myConnection.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection))
{
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
try
{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
if (ex.Number == 2627)
{
// Add a javascript alert to let the user know that the same HESAP value already exists.
}
else
{
// Rethrow the exception.
throw;
}
}
}
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
}
答案 1 :(得分:4)
您可以先添加另一个查询,使用简单的select语句检查密钥是否存在,或者您可以(uglier solution imo)尝试/捕获如果存在主键冲突,ExecuteReader将抛出的SqlException。 / p>
答案 2 :(得分:2)
我假设数据库会同时拥有多个用户。
我建议对主要密钥违规的SQLException进行try / catch,就像Andreas提到的那样。我会建议两个查询,因为应用程序流(来自不同的用户)可能如下所示:
答案 3 :(得分:1)
try
{
//open connection
//Insert
}
catch(SQLException Excep)
{
//If primary key violation alert()
}
finally
{
//Close connection
}
这可能有助于你
答案 4 :(得分:1)
虽然可以而且应该对其他用户(使用语句,ExecuteNonQuery等)所记录的原始代码进行一些改进,但如果你想要做的就是检测主键约束违规,那么你可以检查错误catch块中的SqlException数。错误编号为2627.使用现有代码,它看起来像这样......
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
myConnection.Open();
string hesap = Label1.Text;
string musteriadi = DropDownList1.SelectedItem.Value;
string avukat = DropDownList2.SelectedItem.Value;
SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);
cmd.Parameters.AddWithValue("@HESAP", hesap);
cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
cmd.Parameters.AddWithValue("@AVUKAT", avukat);
cmd.Connection = myConnection;
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
Response.Redirect(Request.Url.ToString());
myConnection.Close();
}
catch (SqlException ex)
{
if (ex.Number == 2627)
{
// Add a javascript alert to let the user know that the same HESAP value already exists.
}
else
{
// Rethrow the exception.
throw;
}
}