首页 > 解决方案 > mysql.data.mysqlclient.mysqlexception 错误 C#

问题描述

在此处输入图像描述

我正在使用 Visual Studio 编写程序。我在登录表单中收到错误。当我搜索互联网。我找不到任何解决方法。我使用 vb.net 时没有任何问题。我在使用 C# 时看到此错误。为什么我看到这个错误? 我的代码:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        MySqlConnection bag = new MySqlConnection("Allow User Variables=True;Server=localhost;Database=gamesh;Uid=root;Pwd='';");

        public Form1()
        {
            InitializeComponent();

        }

        private void Button1_Click(object sender, EventArgs e)
        {
            bag.Open();
            MySqlCommand komut = new MySqlCommand("SELECT * from kullanıcılar where clause KullanicıAdi='" + TextBox1.Text.Trim() + "' and Sifre='" + TextBox2.Text.Trim() + "'", bag);
            MySqlDataReader dr = komut.ExecuteReader();
            if (dr.Read())
            {
                MessageBox.Show("Hoşgeldiniz");
            }
            else
            {
                MessageBox.Show("Hatalı Giriş");
            }
            bag.Close();

        }

    }
}

标签: mysqldatabaseexceptionmysql-error-1064login-script

解决方案


该错误与 C# 无关,您的 SQL 查询语法不正确,如错误消息中所述:

SELECT * from kullanıcılar where子句KullanicıAdi='...

WHERE 后面必须跟一系列字段和参数

此外,不要通过文本连接向查询添加参数 - 使用参数化查询


推荐阅读