17,740
社区成员
发帖
与我相关
我的任务
分享
string connStr="server=XCH;uid=sa;pwd=;database=aaa";//定义数据库连接字符串
SqlConnection conn=new SqlConnection(connStr); //创建连接对像
string sql = "Insert into Table1(id,admin,pwd) values ('"+TextBox1.Text+"','"+username.Text+"','"+user.Text+"') select @@identity ";
SqlCommand comm=new SqlCommand(sql,conn);//创建Command对像
conn.Open();//打开连接
object objID =comm.ExecuteScalar();
comm.CommandText = sql;
try
{
int nRet = (int)comm.ExecuteScalar();
comm.Dispose();
return;
}
catch(Exception ex)
{
Console.WriteLine("-------------------IsRecordExist_Error-------------------)");
Console.WriteLine("IsRecordExist_Error_({0})", sql);
Console.WriteLine("{0}, {1})", ex.GetType().ToString(), ex.Message);
}
Response.Write(objID.ToString());
Response.Write("<script language=javascript>alert('操作成功');window.location.href=window.location.href;</script>");
//关闭数据库
conn.Close();
点击提交按钮后 怎么才能判断我提交的内容在数据表中有无重复呢string connStr="server=WWW-EA89185F163;uid=sa;pwd=;database=YWX";//定义数据库连接字符串
SqlConnection conn=new SqlConnection(connStr); //创建连接对像
string sql = "Insert into Table1(id,admin,pwd) values ('"+TextBox1.Text+"','"+username.Text+"','"+user.Text+"') select @@identity ";
SqlCommand comm=new SqlCommand(sql,conn);//创建Command对像
conn.Open();//打开连接
object objID =comm.ExecuteScalar();
Response.Write(objID.ToString());
Response.Write("<script language=javascript>alert('操作成功');window.location.href=window.location.href;</script>");
//关闭数据库
conn.Close();
string connStr="server=WWW-EA89185F163;uid=sa;pwd=;database=YWX";//定义数据库连接字符串
SqlConnection conn=new SqlConnection(connStr); //创建连接对像
string sql = "Insert into Table1(id,admin,pwd) values ('"+TextBox1.Text+"','"+username.Text+"','"+user.Text+"') select @@identity ";
SqlCommand comm=new SqlCommand(sql,conn);//创建Command对像
conn.Open();//打开连接
object objID =comm.ExecuteScalar();
comm.CommandText = sql;
try
{
int nRet = (int)comm.ExecuteScalar();
comm.Dispose();
return;
}
catch(Exception ex)
{
Console.WriteLine("-------------------IsRecordExist_Error-------------------)");
Console.WriteLine("IsRecordExist_Error_({0})", sql);
Console.WriteLine("{0}, {1})", ex.GetType().ToString(), ex.Message);
Response.Write(" 有相同的!!!");
}
Response.Write(objID.ToString());
Response.Write("<script language=javascript>alert('操作成功');window.location.href=window.location.href;</script>");
//关闭数据库
conn.Close();
INSERT INTO TB SELECT * FROM TB T
WHERE NOT EXISTS(SELECT 1 FROM TB WHERE ID=T.ID)