我当前的代码给了我以下错误,第一次是我添加记录,第二次是我删除或更新记录时。数据库仍然在这些实例中正确更新
要进一步解释,我可以添加像XXX这样的东西,它在开始时没有在数据库中,我得到错误#1,即使数据库已更新。当我删除该条目时,数据库更新,我收到错误#2。如果我再次返回并再次插入XXX,则数据库会更新,我会再次收到错误#1。
编辑#2,我在数据库中注意到UNIQUE KEY
列正在跳过数字,所以我可以添加VVV,发生错误1,得到的UNIQUE KEY
为136.然后添加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);
}
}
答案 0 :(得分:1)
您正在执行SQL两次。第一次执行是在调用
期间xp.ExecuteNonQuery();
这样可行,但是当你调用
时第二次执行SQLda.Fill(ds);
会抛出你看到的错误。
由于您最终需要DataSet,请尝试删除xp.ExecuteNonQuery();
行。
答案 1 :(得分:0)
您可能会再次使用相同的密钥为其提供表条目。 每次执行代码时都会看到值将插入到表中。因此,如果您再次执行它并且每次为主键赋予相同的值,则会出错。
因此,您可以删除旧记录,如果它是多余的,或者您可以使用不同的主键测试您的程序。