本人做的是一个用户注册页面,再添加用户时需要判断一下有没有用户已经注册,如已注册就跳出提示框,没有就添加进去。本人写了一段代码(c#),结果只要按下注册按钮都会跳出已有该用户的提示框,且什么也加不进去。代码如下,希望大虾们多多帮忙~
//第一步打开数据库
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "server=.;uid=sa;pwd=sa;database=text";
conn.Open();
//第二步执行SQL
string sql;
sql = "select *from suerT where username='" + Teb1.Text + "'";
if (sql!="")
{ Response.Write(""); }//判断结果
else
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into suerT(username,passwd,sex,birthday) values('" + Teb1.Text + "','" + Teb2.Text + "','" + Dropsex.SelectedItem.Value.ToString() + "','" + Teb3.Text + "')";
cmd.ExecuteNonQuery();
}
//第三步关闭连接
conn.Dispose();
我总觉得“if (sql!="")”这个有点问题,但就不知道怎么改~请各位大虾帮帮忙,在此谢谢了~
我改了已下,代码如下:
string sql;
sql = "select *from suerT where username='" + Teb1.Text + "'";
SqlDataAdapter Da = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandText = sql;
cmd1.CommandType = CommandType.Text;
cmd1.Connection = conn;
DataSet Ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(Ds, "tt");
if (Ds.Tables[0].Rows.Count.ToString=0)
{ Response.Write(""); }
但“(Ds.Tables[0].Rows.Count.ToString=0)”还是有错误~我刚学的~请哪位大侠刚我改一下~谢谢,也祝你国庆快乐!
|