日期:2014-05-17  浏览次数:20931 次

用 C# 怎么 调用 数据库里的东西 SQL
  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#,编程,数组,结构,讨论

------解决方案--------------------
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("用户名重复");
    }
}

------解决方案--------------------
引用:
Quote: 引用:

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)