在 eecute reader 上收到错误,我的 sql 语法不正确

Receiving error on eecute reader that my sql syntax is incorrect

我猜问题是我在一个命令中有两个单独的查询。有办法解决这个问题吗?

连接字符串中填充了示例数据,但连接正确。

我也知道将连接字符串放在实际文件中是不安全的,但这目前仅用于测试目的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Multiple_forms
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void registerSubmitButton_Click(object sender, EventArgs e)
        {
            string myConnection = "Server=localhost;Database=Houses;Uid=user;Pwd=password;";
            MySqlConnection myConn = new MySqlConnection(myConnection);

            string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + ";" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
                                 "', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
                                 this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
                                 "';";

            string inputPass = this.regPasswordTextBox.Text;
            string inputPassConfirm = this.regPasswordConfirmTextBox.Text;

            MySqlCommand selectCommand = new MySqlCommand(selectQuery, myConn);
            MySqlDataReader myReader;

            if (inputPass == inputPassConfirm)
            {
                myConn.Open();
                myReader = selectCommand.ExecuteReader();



            int regCount = 0;
            while (myReader.Read())
            {
                regCount = regCount + 1;
            }

            if (regCount == 1)
                {
                    MessageBox.Show("You have registered successfully!");
                }
                else
                {
                    MessageBox.Show("Invalid registration key.");
                }
            }
            else
            {
                MessageBox.Show("Passwords don't match.");
                Thread.Sleep(2000);
                this.Close();
            }
        }
    }
}

因为您收到 Sql 语法错误,我怀疑问题出在您的查询 selectQuery 中。正如您在下面看到的,您没有在 this.regKeyTextBox.Text.

之后关闭引号
 string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + ";" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
                      "', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
                      this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
                      "';";

尝试将查询更改为:

string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + "';" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
                      "', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
                      this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
                      "';";

点赞更新

string selectQuery = "Select * from users where RegistrationKey = '" + this.regKeyTextBox.Text + "';" + "UPDATE users set UserName = '" + this.regUsernameTextBox.Text.ToLower() +
                                 "', Password = '" + this.regPasswordTextBox.Text + "', Email ='" +
                                 this.regEmailTextBox.Text + "' WHERE RegistrationKey = '" + this.regKeyTextBox.Text +
                                 "';";