我當前的代碼給我以下錯誤,第一次添加記錄時,第二次當我刪除或更新記錄。在這些情況下,數據庫仍然正確更新。數據庫正確更新,但出現錯誤
要進一步解釋,我可以添加類似XXX的數據,即使數據庫已更新,但數據庫中並不存在數據庫。當我刪除該條目時,數據庫更新,並且出現錯誤#2。如果我再返回並再次插入XXX,則數據庫會更新,並再次出現錯誤#1。
編輯#2,我也注意到在datbase該UNIQUE KEY
列跳躍的數字,所以我可以添加VVV,誤差1時,它得到的136 UNIQUE KEY
然後加真空紫外,得到錯誤#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);
}
}
如果我是一個博彩人,我會說問題是您可以在數據庫中已存在的行上執行「INSERT」。您不應該將'INSERT'插入到數據庫中已有的行中(即有一個鍵)。 –
我可以輸入XXX的測試,它不在數據庫中,但我仍然收到此錯誤。驗證實際記錄不存在後,我在第一個條目中收到此錯誤。 – JLM