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个回答
展开全部
Diobo - 见习魔法师 三级
说的绝对没问题
说的绝对没问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SqlConnection conn = new SqlConnection(@"Server=VIRTUALPC;Integrated Security=true;Database=User;");
conn.Open();
StringBuilder tx = new StringBuilder("Select Count(*) from TableUser where Account = '");
tx.Append(userName);
tx.Append("'");
SqlCommand cmd = new SqlCommand(tx.ToString(), conn);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
LblError.Text = "该用户名已存在";
我直接拷了我的一个程序的代码,你改下连接字符串、命令语句中表名、列名就可以
conn.Open();
StringBuilder tx = new StringBuilder("Select Count(*) from TableUser where Account = '");
tx.Append(userName);
tx.Append("'");
SqlCommand cmd = new SqlCommand(tx.ToString(), conn);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
LblError.Text = "该用户名已存在";
我直接拷了我的一个程序的代码,你改下连接字符串、命令语句中表名、列名就可以
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询