【问题标题】:C# SQL Search result into a string/textboxC# SQL 搜索结果到字符串/文本框
【发布时间】:2015-06-11 06:39:38
【问题描述】:

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

【问题讨论】:

    标签: c# mysql string textbox reader


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      SELECT StudentID FROM Users WHERE StudentID = @ID
      

      【讨论】:

      • 即使列名中有空格?我仍然收到错误
      • 请看 Saagar 的回答 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 2019-04-24
      • 2013-04-03
      • 2010-12-10
      • 1970-01-01
      相关资源
      最近更新 更多