【发布时间】:2014-09-07 20:07:12
【问题描述】:
我一直在试图弄清楚为什么我的程序总是给我错误。 system.data.oledb.oledbexception(0x80040E14): INSERT INTO 语句中的语法错误。
- 表名:
User -
列:
Username AccountNumber FirstName LastName
代码:
namespace Library_System
{
public partial class CreateAccountWindow : Form
{
OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jc\Documents\Visual Studio 2013\Projects\Library System\Library System\LibrarySystemDatabase.accdb;Persist Security Info=False;");
OleDbCommand command = new OleDbCommand();
//OleDbDataReader reader;
public CreateAccountWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string Username = "", AccountNumber = "", FirstName = "", LastName = "";
//int Borrowed = 0;
bool hasValue1 = false, hasValue2 = false, hasValue3 = false, hasValue4 = false;
if (textBox1.Text != "")
{
label1.Hide();
Username = textBox1.Text;
hasValue1 = true;
}
else
{
label1.Show();
label1.Text = "Required";
}
if (textBox10.Text != "")
{
label21.Hide();
AccountNumber = textBox8.Text;
hasValue2 = true;
}
else
{
label21.Show();
label21.Text = "Required";
}
if (textBox8.Text != "")
{
label13.Hide();
FirstName = textBox10.Text;
hasValue3 = true;
}
else
{
label13.Show();
label13.Text = "Required";
}
if (textBox7.Text != "")
{
label12.Hide();
label12.Text = "Required";
LastName = textBox7.Text;
hasValue4 = true;
}
else
{
label12.Show();
label12.Text = "Required";
}
if (hasValue1 || hasValue2 || hasValue3 || hasValue4)
{
try
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "insert into User (Username,AccountNumber,FirstName,LastName) values ('" + Username + "','" + AccountNumber + "','" + FirstName + "','" + LastName + "')";
command.ExecuteNonQuery();
MessageBox.Show("REGISTRATION COMPLETE !!", "DONE");
connect.Close();
}
catch (Exception ex)
{
connect.Close();
MessageBox.Show("Error:"+ex.ToString());
}
}
}
}
【问题讨论】:
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入
-
抱歉,您能说得更具体点吗?我刚刚开始使用 c# 3 周。我不太懂技术术语
-
@JC Balantakbo,
AccountNumber字段类型是什么? -
@JCBalantakbo marc_s 的意思是您不应该通过连接字符串来创建 SQL 查询。例如,如果您的用户名类似于
a','b','c','d');drop table User;--,请考虑将在数据库中执行什么。恶意用户可能会将其作为用户名来入侵您的系统。
标签: c# insert oledb ms-access-2013