【发布时间】:2019-07-15 16:04:34
【问题描述】:
我是使用 Visual Basic Community 2015 学习 C# 和 MySql 的新手,我正在尝试制作简单的 CRUD,但是当我运行程序并尝试将数据输入到 MySql 表时我很困惑,因为它总是显示消息
MySql.Data.dll 中出现“MySql.Data.MySqlClient.MySqlException”类型的未处理异常
补充资料:
您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在 'Siswa,Total Biaya SPP,Sisa Bayar SPP,Keterangan) VALUES 附近使用的正确语法
有什么解决办法吗?
public partial class Crud : Form
{
MySqlConnection conn = new MySqlConnection("Server=localhost;User Id=root;Password='';Database=db_csharp1");
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand();
public DataSet ds = new DataSet();
public Crud()
{
InitializeComponent();
}
private void Crud_Load(object sender, EventArgs e)
{
GetRecords();
}
private void btnTambah_Click(object sender, EventArgs e)
{
ds = new DataSet();
adapter = new MySqlDataAdapter ("INSERT INTO siswa (NIS,Nama Siswa,Total Biaya SPP,Sisa Bayar SPP,Keterangan) VALUES ('"+textNIS.Text+"','"+textNamaSiswa.Text+"','"+textBiayaSPP.Text+"','"+textSisaBayar.Text+"','"+textKeterangan+"')", conn);
adapter.Fill(ds,"siswa");
MessageBox.Show("Added!");
textNIS.Clear();
textNamaSiswa.Clear();
textBiayaSPP.Clear();
textSisaBayar.Clear();
textKeterangan.Clear();
GetRecords();
}
private void GetRecords()
{
ds = new DataSet();
adapter = new MySqlDataAdapter("select * from siswa", conn);
adapter.Fill(ds, "siswa");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "siswa";
}
【问题讨论】:
-
请为您的 SQL 语句使用参数化语句,否则您的代码容易受到 SQL 注入攻击。从您发布的错误来看,您的 Insert 语句不正确。