private void txtpwd_TextChanged(object sender, EventArgs e)
        {
            string a = txtname.Text;
            string b = "select Uname from userinfo where Uname =a";            if (a == b)
            {
                MessageBox.Show("用户名重复");
            }
        }
这个我的代码。。
我总觉得
b = "select Uname from userinfo where Uname =a";
这有错误。。
怎么能把用户输入的结果  当成 where条件给他。。我其实还想用定义个数组  把select Uname from userinfo的查询结果都放到数组里。。但是 不会定义数组
菜鸟求教。。谢谢了c#,编程,数组,结构,讨论

解决方案 »

  1.   

    SqlConnection conn = ...
    SqlCommand sqlCommand = ...
    sql = "select Uname from userinfo where Uname ='" + a + "'";
    string b = sqlCommand.ExecuteScalar(sql, conn).ToString();
    ...
      

  2.   

    public static bool CheckSameName(string UserName)
    {
        string sql = "SELECT * FROM userinfo WHERE UserName = @UserName ";    SqlCommand command = new SqlCommand(sql);
        command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = UserName;
        DataTable table = DBProvider.DefaultDBOperator.GetDataTable(command);
        if (table.Rows.Count == 0)
        {
            return true;
        }
        return false;
    }private void txtpwd_TextChanged(object sender, EventArgs e)
    {
        string a = txtname.Text;
        bool blnSameName=CheckSameName(a);    if (blnSameName)
        {
            MessageBox.Show("用户名重复");
        }
    }
      

  3.   

    不用 拼串的。。老师说那个有漏洞 防 SQL注入
      

  4.   

     DataTable table = DBProvider.DefaultDBOperator.GetDataTable(command);
    求解看不太懂
      

  5.   

    要防SQL注入那你写存储过程来登陆,没想到你老师那么专业,居然会有要求的