mysql - 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();
}
}
}
解决方案
该错误与 C# 无关,您的 SQL 查询语法不正确,如错误消息中所述:
SELECT * from kullanıcılar where子句KullanicıAdi='...
WHERE 后面必须跟一系列字段和参数
此外,不要通过文本连接向查询添加参数 - 使用参数化查询
推荐阅读
- javascript - 来自 Axios GET 响应的 VueJs 中的数组为空 | VueJS/LARAVEL
- java - Android 应用更新对话框(没有 Google Play),解析错误
- apache - 替换 url htaccess 中的目录
- python - 如何使用 Keras 在第 k 层之后获得激活?
- javascript - D3 Force-Directed Tree Set 节点深度
- complex-event-processing - Siddhi 查询在时间限制内先于另一个事件
- php - 根据位置执行查询成表(单元格)
- optaplanner - 在 OptaPlanner 中,如何限制将事实分配给实体的次数?
- c++ - 如何使用基于范围的 for 循环遍历 C++ 中可变的重复 Proto 字段?
- python - Python如何找到第二个连接?