违反了 PRIMARY KEY 约束 'PK_User'。不能在对象 'User' 中插入重复键。
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingS...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class User_page3 : System.Web.UI.Page
{
public void aa()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [User]";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds,"[User]");
con.Close();
this.GridView1.DataSourceID = null;
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
this.aa();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into [User] values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已保存!\");</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from [User] where id="+ this.TextBox1.Text + "','" + this.TextBox2.Text + " ";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已删除!\");</script>");
}
} 展开
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class User_page3 : System.Web.UI.Page
{
public void aa()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [User]";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds,"[User]");
con.Close();
this.GridView1.DataSourceID = null;
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
this.aa();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into [User] values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已保存!\");</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from [User] where id="+ this.TextBox1.Text + "','" + this.TextBox2.Text + " ";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已删除!\");</script>");
}
} 展开
8个回答
展开全部
你的数据库中的主键列id已经有一个数据刚好等于this.TextBox1.Text;所以你对主键列重复插入相同的值是会报错的。请允许我在你的代码中加多一个方法来判断数据库的主键列id中是否已经存在 this.TextBox1.Text 。
//以下代码用于判断你的主键列中是否存在this.TextBox1.Text;
public bool select(string text)
{
bool contains = false;
List<string> list = new List<string>();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select id from [User]";
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(dr[0].ToString());
}
dr.Close();
con.Close();
if (list.Contains(text))
{
contains = true;
}
return contains;
}
****************************方法到此结束*******************************
//调用写好的方法
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into [User] values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')";
cmd.Connection = con;
con.Open();
//以下是在你插入时的代码里面加入的判断代码
if(select(this.TextBox1.Text))
{
MessageBox.Show(this.TextBox1.Text+"在数据库中已经存在!");
return;
}
//以上是在你插入时的代码里面加入的判断代码
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已保存!\");</script>");
}
现在就已经排除了相同主键报错的可能性。
2,设置主键自增
这个方法是你让主键列自增,然后插入的语句是:"insert into [User] values('" + this.TextBox2.Text + "')";这样更加简单,你考虑一下吧
//以下代码用于判断你的主键列中是否存在this.TextBox1.Text;
public bool select(string text)
{
bool contains = false;
List<string> list = new List<string>();
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select id from [User]";
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(dr[0].ToString());
}
dr.Close();
con.Close();
if (list.Contains(text))
{
contains = true;
}
return contains;
}
****************************方法到此结束*******************************
//调用写好的方法
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "insert into [User] values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')";
cmd.Connection = con;
con.Open();
//以下是在你插入时的代码里面加入的判断代码
if(select(this.TextBox1.Text))
{
MessageBox.Show(this.TextBox1.Text+"在数据库中已经存在!");
return;
}
//以上是在你插入时的代码里面加入的判断代码
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert(\"已保存!\");</script>");
}
现在就已经排除了相同主键报错的可能性。
2,设置主键自增
这个方法是你让主键列自增,然后插入的语句是:"insert into [User] values('" + this.TextBox2.Text + "')";这样更加简单,你考虑一下吧
展开全部
你的数据库中的主键列id已经有一个数据刚好等于this.TextBox1.Text;所以你对主键列重复插入相同的值是会报错的。请允许我在你的代码中加多一个方法来判断数据库的主键列id中是否已经存在 this.TextBox1.Text 。<br>//以下代码用于判断你的主键列中是否存在this.TextBox1.Text;<br>public bool select(string text) <br> {<br> bool contains = false;<br> List<string> list = new List<string>();<br> SqlConnection con = new SqlConnection();<br> con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";<br> SqlCommand cmd = new SqlCommand();<br> cmd.CommandText = "select id from [User]";<br> cmd.Connection = con;<br> con.Open();<br> SqlDataReader dr = cmd.ExecuteReader();<br> while (dr.Read()) <br> {<br> list.Add(dr[0].ToString());<br> }<br> dr.Close();<br> con.Close();<br> if (list.Contains(text))<br> {<br> contains = true;<br> }<br> return contains;<br> }<br><br>****************************方法到此结束*******************************<br><br>//调用写好的方法<br>protected void Button1_Click(object sender, EventArgs e)<br> {<br> SqlConnection con = new SqlConnection();<br> con.ConnectionString = "server=(local);uid=sa;pwd=password;database=ss;";<br> SqlCommand cmd = new SqlCommand();<br> cmd.CommandText = "insert into [User] values('" + this.TextBox1.Text + "','" + this.TextBox2.Text + "')";<br> cmd.Connection = con;<br> con.Open();<br><br> //以下是在你插入时的代码里面加入的判断代码<br> if(select(this.TextBox1.Text))<br> {<br> MessageBox.Show(this.TextBox1.Text+"在数据库中已经存在!");<br> return;<br> }<br> //以上是在你插入时的代码里面加入的判断代码<br><br>cmd.ExecuteNonQuery();<br> con.Close();<br> Response.Write("<script>alert(\"已保存!\");</script>");<br> }<br>现在就已经排除了相同主键报错的可能性。<br><br>2,设置主键自增<br>这个方法是你让主键列自增,然后插入的语句是:"insert into [User] values('" + this.TextBox2.Text + "')";这样更加简单,你考虑一下吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在插入数据之前先检查一下主键是否存在!以避免插入重复的主键,违反主键约束
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
检查你的表主键有没有设置标示值或是自增加。不然相同的主键就会暴这个错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这表示你的这个表里试图插入重复的记录,重复就重复在主键的字段上。即 Primary Key。所以报错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |