数据库正在正确更新,但出现错误
本文关键字:错误 更新 数据库 | 更新日期: 2023-09-27 18:03:06
我当前的代码给了我以下错误,第一个是当我添加记录时,第二个是当我删除或更新记录时。在这些实例中,数据库仍然正确更新
为了进一步解释,我可以添加像XXX这样的东西,它在开始时不在数据库中,即使数据库更新了,我也得到了错误#1。当我删除该条目时,数据库更新,得到错误#2。如果我返回并再次插入XXX,数据库更新,我再次得到错误#1。
编辑#2,我确实注意到在数据库中,UNIQUE KEY
列跳过数字,所以我可以添加VVV,错误1发生,它得到136的UNIQUE KEY
。然后添加VUV,得到错误#1,UNIQUE KEY
为138
违反UNIQUE KEY
约束AK_DimCurrency_CurrencyAlternateKey
。不能在对象dbo.DimCurrency
中插入重复键。重复键值为(XXX),语句终止。
IListSource
不包含任何数据源。
public partial class Default : System.Web.UI.Page
{
SqlConnection vid = new SqlConnection("Data Source=(LocalDB)''v11.0;AttachDbFilename=''AdventureWorksDW_Data.mdf;Integrated Security=True;Connect Timeout=30");
static DataTable dtDataForGrid = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonSearch_Click(object sender, EventArgs e)//on button click the database connection is opened and the query is executed if possible, if not error will display on label 1
{
LabelError.Text = "";
try
{
string str = TextBox1.Text;
SqlCommand xp = new SqlCommand(str, vid);
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
}
catch (Exception c)
{
LabelError.Text = (c.Message);
}
}
protected void ButtonReset_Click(object sender, EventArgs e)//resets the page, if error, it will display at label 1
{
try
{
Response.Redirect("~/Default.aspx");
}
catch (Exception c)
{
LabelError.Text = (c.Message);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)//drop down list that adds text depending on choice to reduce redundant typing in multiple executions
{
if (DropDownList1.SelectedIndex == 1)
{
TextBox2.Text = "INSERT INTO";
TextBox3.Text = " VALUES";
TextBox4.Text = "";
TextBox4.Enabled = false;
}
if (DropDownList1.SelectedIndex == 2)
{
TextBox2.Text = "UPDATE";
TextBox3.Text = " SET";
TextBox4.Text = " WHERE";
TextBox4.Enabled = true;
}
if (DropDownList1.SelectedIndex == 3)
{
TextBox2.Text = "DELETE FROM";
TextBox3.Text = " WHERE";
TextBox4.Text = "";
TextBox4.Enabled = false;
}
}
protected void ButtonChange_Click(object sender, EventArgs e)//executes the operation from textbox2, and 3.
{
LabelError2.Text = "";//resets the label to nothing before changing the text again if needed below
LabelFullOperation.Text = "";//resets the label to nothing before changing the text again if needed below
if (TextBox4.Text == null)//sets the label6 text to textbox 2 and 3, unless 4 has text, in which case 2, 3, and 4 will be used
{
LabelFullOperation.Text = TextBox2.Text + TextBox3.Text;
}
else
{
LabelFullOperation.Text = TextBox2.Text + TextBox3.Text + TextBox4.Text;
}
try
{
string str = TextBox2.Text + TextBox3.Text + TextBox4.Text;//gets the text from textboxes 2, 3, adn 4, if any
SqlCommand xp = new SqlCommand(str, vid);
vid.Open();
xp.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
}
catch (Exception c)
{
LabelError2.Text = (c.Message);
}
}
您执行了两次SQL。它第一次执行是在调用
期间。xp.ExecuteNonQuery();
这可以工作,但是当您调用
时,SQL将第二次执行。da.Fill(ds);
会抛出您所看到的错误。
因为你想要一个DataSet在最后,尝试删除xp.ExecuteNonQuery();
行
您可能会再次使用相同的键给它表项。每次执行代码时,值都会被插入到表中。因此,如果您再次执行它并每次为主键提供相同的值,它将给出一个错误。
所以你可以删除旧的记录,如果它是多余的,或者你可以用一个不同的主键测试你的程序。