插入按钮语句
protected void Button1_Click(object sender, EventArgs e)
{
cn = new OleDbConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ToString();
string sqlstr = "select * from UserInfo where username='"+TextBox1.Text.Trim()+"' ";
cn.Open();
OleDbCommand cmd = new OleDbCommand(sqlstr,cn);
OleDbDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
if (this.TextBox1.Text.Trim() == read["username"].ToString().Trim())
{
Response.Write("<script language='javascript'>alert('该会员已经注册过');localtion='Register.aspx'</script>");
return;
}
}
read.Close();
string addUser = "insert into UserInfo(Username,Password,Email,QQ) values('"+TextBox1.Text.Trim()+"','"+TextBox2.Text.Trim()+"','"+TextBox4.Text.Trim()+"',"+TextBox5.Text.Trim()+")";
OleDbCommand cmdadd = new OleDbCommand(addUser,cn);
int res = 0;
try
{
res = cmdadd.ExecuteNonQuery();
}
catch
{
}
finally
{
cn.Close();
}
if (res > 0)
{
Response.Write("<script>alert('注册成功');</script>");
OleDbCommand cmd1 = new OleDbCommand("select UserID from UserInfo where username='" + TextBox1.Text.Trim() + "'");
OleDbDataReader read1 = cmd1.ExecuteReader();
read.Read();
Session["UserID"] = read1["UserID"].ToString();
Session["Username"] = read1["username"].ToString();
Session["Password"] = read1["password"].ToString();
Response.Redirect("SuccReg.aspx"); //跳转
}
else
Response.Write("<script>alert('注册失败');</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text == string.Empty)
{
Response.Write("<script>alert('会员登录名不能为空!');location='Register.aspx'</script>");
}
else
{
cn = new OleDbConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ToString();
cn.Open();
string sqlstr = "select * from UserInfo where username='"+TextBox1.Text.Trim()+"' ";
OleDbCommand cmd = new OleDbCommand(sqlstr,cn);
OleDbDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
if (this.TextBox1.Text.Trim() == read["username"].ToString().Trim())
{
Response.Write("<script>alert('该会员已经注册过');location='Register.aspx'</script>");
return;
}
}
else
{
Response.Write("<script>alert('您可以正常注册');</script>");
}
read.Close();
cn.Close();
}
}
UserInfo 表中有一个 以 UserID为主键的列,我没有列入插入对象。
然后我执行的时候,插入不成功,到底是哪里的问题呢?我的UserID不知道怎样设置才能够自增,还有 Insert 语句是否有错呢,大家注意是ACCESS表喔
protected void Button1_Click(object sender, EventArgs e)
{
cn = new OleDbConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ToString();
string sqlstr = "select * from UserInfo where username='"+TextBox1.Text.Trim()+"' ";
cn.Open();
OleDbCommand cmd = new OleDbCommand(sqlstr,cn);
OleDbDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
if (this.TextBox1.Text.Trim() == read["username"].ToString().Trim())
{
Response.Write("<script language='javascript'>alert('该会员已经注册过');localtion='Register.aspx'</script>");
return;
}
}
read.Close();
string addUser = "insert into UserInfo(Username,Password,Email,QQ) values('"+TextBox1.Text.Trim()+"','"+TextBox2.Text.Trim()+"','"+TextBox4.Text.Trim()+"',"+TextBox5.Text.Trim()+")";
OleDbCommand cmdadd = new OleDbCommand(addUser,cn);
int res = 0;
try
{
res = cmdadd.ExecuteNonQuery();
}
catch
{
}
finally
{
cn.Close();
}
if (res > 0)
{
Response.Write("<script>alert('注册成功');</script>");
OleDbCommand cmd1 = new OleDbCommand("select UserID from UserInfo where username='" + TextBox1.Text.Trim() + "'");
OleDbDataReader read1 = cmd1.ExecuteReader();
read.Read();
Session["UserID"] = read1["UserID"].ToString();
Session["Username"] = read1["username"].ToString();
Session["Password"] = read1["password"].ToString();
Response.Redirect("SuccReg.aspx"); //跳转
}
else
Response.Write("<script>alert('注册失败');</script>");
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text == string.Empty)
{
Response.Write("<script>alert('会员登录名不能为空!');location='Register.aspx'</script>");
}
else
{
cn = new OleDbConnection();
cn.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ToString();
cn.Open();
string sqlstr = "select * from UserInfo where username='"+TextBox1.Text.Trim()+"' ";
OleDbCommand cmd = new OleDbCommand(sqlstr,cn);
OleDbDataReader read = cmd.ExecuteReader();
read.Read();
if (read.HasRows)
{
if (this.TextBox1.Text.Trim() == read["username"].ToString().Trim())
{
Response.Write("<script>alert('该会员已经注册过');location='Register.aspx'</script>");
return;
}
}
else
{
Response.Write("<script>alert('您可以正常注册');</script>");
}
read.Close();
cn.Close();
}
}
UserInfo 表中有一个 以 UserID为主键的列,我没有列入插入对象。
然后我执行的时候,插入不成功,到底是哪里的问题呢?我的UserID不知道怎样设置才能够自增,还有 Insert 语句是否有错呢,大家注意是ACCESS表喔
你百度下.NET 插入 access的例子吧 争取自己解决了。太晚了睡了
UserID是主键,而且是自动增长
cmd.Parameters.AddWithValue("@UserName", "xxxx");
cmd.Parameters.AddWithValue("@Password", "xxx");
cmd.Parameters.AddWithValue("@email", "xxxx");
cmd.ExecuteNonQuery();另外注意mdb文件的安全性设置允许匿名可修改
尝试了这方法,能插入,但在Catch 中还是抓取了异常,
cmdadd.Parameters.AddWithValue("@Username", TextBox1.Text.Trim());
cmdadd.Parameters.AddWithValue("@Password", TextBox2.Text.Trim());
cmdadd.Parameters.AddWithValue("@Email", TextBox4.Text.Trim());
cmdadd.Parameters.AddWithValue("@QQ", TextBox5.Text.Trim());
res = cmdadd.ExecuteNonQuery();
if (res == 1)
{
Response.Write("<script>alert('注册成功');</script>");
OleDbCommand cmd1 = new OleDbCommand("select UserID from UserInfo where username='" + TextBox1.Text.Trim() + "'", cn);
OleDbDataReader read1 = cmd1.ExecuteReader();
read1.Read();
Session["UserID"] = read1["UserID"].ToString();
Session["Username"] = read1["username"].ToString();
Session["Password"] = read1["password"].ToString();
Response.Redirect("SuccReg.aspx"); //跳转
跳转语句不执行
异常报告的是什么错误?