C#插入数据库,不能重复!!
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;
using System.Data.OleDb;
public partial class usermanage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string uid = this.TextBox1.Text;
string upwd = this.TextBox2.Text;
string city = this.TextBox3.Text;
string uname = this.TextBox4.Text;
string ConnString = "server=.;database=service;user id=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
conn.Open();
string sel = string.Format("select uid from userinfo where uid = uid");
string sql = string.Format("Insert Into userinfo(uid,upwd,city,uname) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','"+TextBox4.Text+"')", uid, upwd, city,uname);
comm.CommandText = sql;
comm.ExecuteNonQuery();
conn.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;
using System.Data.OleDb;
public partial class usermanage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string uid = this.TextBox1.Text;
string upwd = this.TextBox2.Text;
string city = this.TextBox3.Text;
string uname = this.TextBox4.Text;
string ConnString = "server=.;database=service;user id=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
conn.Open();
string sel = string.Format("select uid from userinfo where uid = uid");
string sql = string.Format("Insert Into userinfo(uid,upwd,city,uname) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','"+TextBox4.Text+"')", uid, upwd, city,uname);
comm.CommandText = sql;
comm.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('添加成功!')</script>");
}
}
要提示已经存在该用户,该怎么修改??? 展开
7个回答
展开全部
protected void submit_Click(object sender, EventArgs e)
{
string code;
code = Convert.ToString(this.TextBox1.Text);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"'", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
code = regex.Replace(code, " ");
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=CodeRead;uid=sa;pwd= ");
conn.Open();
string sql = "select Count(1) from code where numble=('" + code + "')";
SqlCommand cmd1 = new SqlCommand(sql, conn);
int count = (int)cmd1.ExecuteScalar();
if (count > 1)
Response.Write("数据重复!");
else
{
SqlCommand cmd = new SqlCommand("insert into code(numble) values('" + code + "')", conn);
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() > 0)
Response.Write(code + "数据导入成功!");
else
Response.Write("数据导入错误!");
conn.Close();
}
展开全部
protected void submit_Click(object sender, EventArgs e)
{
string code;
code = Convert.ToString(this.TextBox1.Text);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"'", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
code = regex.Replace(code, " ");
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=CodeRead;uid=sa;pwd= ");
conn.Open();
string sql = "select Count(1) from code where numble=('" + code + "')";
SqlCommand cmd1 = new SqlCommand(sql, conn);
int count = (int)cmd1.ExecuteScalar();
if (count > 1)
Response.Write("数据重复!");
else
{
SqlCommand cmd = new SqlCommand("insert into code(numble) values('" + code + "')", conn);
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() > 0)
Response.Write(code + "数据导入成功!");
else
Response.Write("数据导入错误!");
conn.Close();
}
{
string code;
code = Convert.ToString(this.TextBox1.Text);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"'", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
code = regex.Replace(code, " ");
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=CodeRead;uid=sa;pwd= ");
conn.Open();
string sql = "select Count(1) from code where numble=('" + code + "')";
SqlCommand cmd1 = new SqlCommand(sql, conn);
int count = (int)cmd1.ExecuteScalar();
if (count > 1)
Response.Write("数据重复!");
else
{
SqlCommand cmd = new SqlCommand("insert into code(numble) values('" + code + "')", conn);
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() > 0)
Response.Write(code + "数据导入成功!");
else
Response.Write("数据导入错误!");
conn.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用
try//未出错时执行的语句
{
}
catch(Exception ex)//出错是执行的语句
{
Console.WriteLine("!!Error----" + ex.Message);
}
finally//无论出错与不出错始终执行的语句
{
}
用这个方法找找看是什么问题,如果不好用最好把这段代码抽成方法再试就好了.
try//未出错时执行的语句
{
}
catch(Exception ex)//出错是执行的语句
{
Console.WriteLine("!!Error----" + ex.Message);
}
finally//无论出错与不出错始终执行的语句
{
}
用这个方法找找看是什么问题,如果不好用最好把这段代码抽成方法再试就好了.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还有一种方法,在数据库里把用户名设置成主键,然后用
try
{
这里填上你要填加入数据库的内容;
}
catch
{
Response.Write("<script>alert('用户名已存在,请重新填写!')</script>");
}
去捕捉错误
try
{
这里填上你要填加入数据库的内容;
}
catch
{
Response.Write("<script>alert('用户名已存在,请重新填写!')</script>");
}
去捕捉错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string uid = this.TextBox1.Text;
string upwd = this.TextBox2.Text;
string city = this.TextBox3.Text;
string uname = this.TextBox4.Text;
string ConnString = "server=.;database=service;user id=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
conn.Open();
string sql = "select count(*) from userinfo where uname='" + uname + "' and uid='"+uid+"'";
comm.CommandText = sql;
int count = (int)comm.ExecuteScalar();
conn.Close();
if (count > 0)
{
Response.Write("<script>alert('用户已经存在!')</script>");
}
string upwd = this.TextBox2.Text;
string city = this.TextBox3.Text;
string uname = this.TextBox4.Text;
string ConnString = "server=.;database=service;user id=sa;pwd=";
SqlConnection conn = new SqlConnection(ConnString);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
conn.Open();
string sql = "select count(*) from userinfo where uname='" + uname + "' and uid='"+uid+"'";
comm.CommandText = sql;
int count = (int)comm.ExecuteScalar();
conn.Close();
if (count > 0)
{
Response.Write("<script>alert('用户已经存在!')</script>");
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询