C# 数据库正在正确更新,但出现错误
我当前的代码给了我以下错误,第一个是在添加记录时,第二个是在删除或更新记录时。在这些实例中,数据库仍能正确更新 为了进一步解释,我可以添加类似XXX的东西,它在开始时不在数据库中,我得到了错误#1,即使数据库已更新。当我删除该条目时,数据库会更新,并出现错误#2。如果我返回并再次插入XXX,数据库将更新,并且再次出现错误#1 编辑#2,我确实注意到在数据库中,C# 数据库正在正确更新,但出现错误,c#,asp.net,sql,C#,Asp.net,Sql,我当前的代码给了我以下错误,第一个是在添加记录时,第二个是在删除或更新记录时。在这些实例中,数据库仍能正确更新 为了进一步解释,我可以添加类似XXX的东西,它在开始时不在数据库中,我得到了错误#1,即使数据库已更新。当我删除该条目时,数据库会更新,并出现错误#2。如果我返回并再次插入XXX,数据库将更新,并且再次出现错误#1 编辑#2,我确实注意到在数据库中,UNIQUE KEY列跳过了数字,因此我可以添加VVV,出现错误1,它得到136的UNIQUE KEY。然后添加VUV,获取错误#1,唯一
UNIQUE KEY
列跳过了数字,因此我可以添加VVV,出现错误1,它得到136的UNIQUE KEY
。然后添加VUV,获取错误#1,唯一键为138
违反了UNIQUE KEY
约束AK_DimCurrency\u 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();
这是可行的,但是当您调用
da.Fill(ds);
这会抛出您看到的错误
由于最终需要一个数据集,请尝试删除xp.ExecuteNonQuery()代码>行。如果我是一个赌徒,我会说问题是您可以对数据库中已经存在的行执行INSERT
。您不能将
插入到数据库中已经存在的行中(即有一个键)。我可以输入数据库中没有的XXX测试,但仍然会出现此错误。在验证实际记录不存在后,我在第一个条目上收到此错误。我可以添加一个XXX测试,数据库使用该条目更新,然后我得到错误。我删除了条目,数据库更新,我得到了ILIST错误,当我再次插入XXX时,我又得到了第一个错误。如果您检查了XXX,那么在一个空表中,ILIST错误就会出现。删除该行确实修复了数字跳转和错误#1。错误#2仍然发生在除基本搜索之外的任何操作中。@JLM,请发布异常名称和错误#2的详细信息。IListSource不包含任何数据源。我刚想出来。这与数据源代码有关哦,这是出于设计UPDATE
和DELETE
命令不会返回它们更改的记录SELECT
是在数据集中加载数据并将其绑定到网格视图的唯一命令。这就是为什么在数据适配器上调用属性SelectCommand
。