C#SQL搜索结果到字符串/文本框中

时间:2015-04-06 19:48:15

标签: c# mysql string textbox reader

我正在尝试创建注册表单。我想进行验证检查,如果文本框中的ID已经存在,它将显示错误信息。但是,每当我尝试搜索ID并将其放入字符串时,我都会收到错误

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll

这是我的代码:

public SignUpForm()
    {
        InitializeComponent();
    }
    // Connection String
    SqlCeConnection connect = new SqlCeConnection("Data Source= Accounts.sdf;Persist Security Info=false;");   

    private void btnRegister_Click(object sender, EventArgs e) {
        String verifyID = "";

        connect.Open();

        using (SqlCeCommand cmd = new SqlCeCommand("SELECT Student ID FROM Users WHERE Student ID = @ID", connect)) 
        {
            cmd.Parameters.AddWithValue("@ID", txtID.Text);
            using (SqlCeDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    verifyID = (string)reader["Student ID"];
                }
            }
        }

        txtTEMP.Text = verifyID;

2 个答案:

答案 0 :(得分:2)

您的SQL SELECT [Student ID] FROM Users WHERE Student ID = @ID似乎不对。它应该是

SELECT [Student ID] FROM Users WHERE [Student ID] = @ID

答案 1 :(得分:0)

可能错误在您的SQL语句中。考虑将其更改为:

SELECT StudentID FROM Users WHERE StudentID = @ID