昏睡渐醒 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)
    已经注册

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类