昏睡渐醒 2018-12-21 15:42 采纳率: 60%
浏览 365
已采纳

请问Text字符怎样和SqlDataReader定义的量比较?

请问Text字符怎样和SqlDataReader定义的量比较?我在做一个注册窗口,SqlDataReader dr = sel.ExecuteReader();这句定义的dr,怎么用它遍历的值和TextBox.Text比较?这是为了确定数据库中没有TextBox1的,求教。

protected void Button1_Click(object sender, EventArgs e)
{
String connectionstr = "server=.\SQLEXPRESS;database=mysql;integrated security=sspi";
SqlConnection myconnection = new SqlConnection(connectionstr);
try
{
string myselect = "select users from client";
SqlCommand sel = new SqlCommand(myselect);
sel.Connection = myconnection;

myconnection.Open();
SqlDataReader dr = sel.ExecuteReader();
while (dr.Read())
{
//Response.Write(dr[0]);
if ((TextBox1.Text.Equals(dr[0])) == true)
{
Response.Write("alert('该用户名已注册!')");
}
else
{
string myinsert = "insert into client(users,password) values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
SqlCommand ins = new SqlCommand(myinsert);
ins.Connection = myconnection;
ins.ExecuteNonQuery();
}
}
myconnection.Close();

    }
    catch (Exception)
    {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language=javascript>alert('失败了');</Script>");
    }
  • 写回答

1条回答 默认 最新

  • threenewbee 2018-12-21 08:22
    关注

    不需要遍历
    可以这么写
    string myselect = "select count(*) from client where users = '" + textBox1.Text+ "'";
    SqlCommand sel = new SqlCommand(myselect);
    sel.Connection = myconnection;
    myconnection.Open();
    int n = (int)sel.ExecuteScalar();
    if (n > 0)
    已经注册

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码