【发布时间】:2015-09-29 00:28:36
【问题描述】:
我正在尝试为我们的电影数据库创建一个注册。我正在尝试从我们创建的 MS Access 建立连接。但是每当我运行我的代码时,我都会得到一个错误。我正在使用 Visual Studio Express 2012 C#。
为什么我的代码会触发这个错误?
“'System.Data.OleDb.OleDbException' 类型的未处理异常 System.Data.dll 中发生附加信息:语法错误 INSERT INTO 语句。”
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace BigScreen
{
public partial class sign_up : Form
{
private OleDbConnection connection = new OleDbConnection();
public sign_up()
{
InitializeComponent();
}
private void sign_up_Load(object sender, EventArgs e)
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\raizel\Desktop\DataBase\Movie_Database.accdb;
Persist Security Info=False;";
}
private void sign_up_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 thisform = new Form1();
thisform.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "Insert into User ([firstname], [lastname], [username], [password]) values ('" + textBox2.Text + "','" + textBox5.Text + "','" + textBox4.Text + "','" + textBox1.Text + "')";
command.ExecuteNonQuery();
userID++;
MessageBox.Show("Data Saved!");
connection.Close();
}
}
}
【问题讨论】:
-
也许您的文本框有垃圾/无数据导致语法错误。顺便说一句,该代码大量对 SQL 注入开放
-
我很确定在点击保存之前我已经输入了值。
-
然后检查您的完整 SQL 字符串。您得到的错误很清楚,其中存在 SQL 语法错误。
-
我好像真的找不到。这段代码之前完美运行。
-
任何输入中的单引号都会导致错误。