0

我正在尝试创建一个注册表单。我想进行验证检查,如果文本框中的 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;
4

2 回答 2

2

您的 SQLSELECT [Student ID] FROM Users WHERE Student ID = @ID似乎不正确。它应该是

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

于 2015-04-06T19:52:01.970 回答
0

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

SELECT StudentID FROM Users WHERE StudentID = @ID
于 2015-04-06T19:50:13.173 回答