C#SQL INSERT INTO语法错误?
问题:
我有一个带有按钮的WPF应用程序,单击该按钮将创建一个用户名和密码并将其插入到我的数据库中.该数据库是在Microsoft Access 2007中创建的.问题是当我编译并运行该数据库时,出现错误消息"INSERT INTO命令中的语法错误",或达到此目的的某个错误.关键是,我有一个迫切需要答案的问题.
信息:
操作系统:Windows 7 Ultimate-x64
编译器:Visual Studio 2010 Ultimate
语言:C#4.0 WPF
数据库应用程序:Microsoft Access 2007
我正在使用SQL,而不是MySQL
我的代码:
Problem:
I have a WPF Application that has a button that when clicked will create a username and password and insert it into my database. The database was created in Microsoft Access 2007. The problem is when I compile and run it I get an error that says "Syntax Error in INSERT INTO command", or something to that effect. Point being, I have a problem that is in desperate need of an answer.
Info:
OS: Windows 7 Ultimate - x64
Compiler: Visual Studio 2010 Ultimate
Language: C# 4.0 WPF
Database Application: Microsoft Access 2007
I''m using SQL, not MySQL
My Code:
if (strDesiredPassword.Text == strDesiredPassword2.Text && String.IsNullOrWhiteSpace(strDesiredUser.Text) == false)
{
string insertCMD = "INSERT INTO LoginTable (User,Pass) VALUES (@User,@Pass)";
string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\MedDesk\Login.accdb;";
using (OleDbConnection MyConn = new OleDbConnection(ConnStr))
{
try
{//insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
MyConn.Open();
OleDbCommand Cmd = new OleDbCommand(insertCMD, MyConn);
Cmd.Parameters.Add("@User", OleDbType.Char).Value = strDesiredUser.Text;
Cmd.Parameters.Add("@Pass", OleDbType.Char).Value = strDesiredPassword2.Text;
Cmd.ExecuteNonQuery();
MyConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
我怀疑User是Access中的保留字-因此您不能按原样使用它,需要将它括起来方括号
I suspect that User is a reserved word in Access - so you can''t use it as is, you need to surround it with square brackets
string insertCMD = "INSERT INTO LoginTable ([User],Pass) VALUES (@User,@Pass)";
可能会做到
will probably do it