[英]Insert into database table with OLE DB .NET Provider won't work
I am getting an error msg of "syntax error" for the INSERT INTO
command when it gets to cmd.ExecuteNonQuery
. 当我到达cmd.ExecuteNonQuery
时,我得到了INSERT INTO
命令的“语法错误”错误消息。
It is important that I use string.Format
, and that the structure stays as close to the current structure as possible. 重要的是,我使用string.Format
,并且该结构应保持与当前结构尽可能接近。
{
OleDbConnection con = DAL.GetConnection();
con.Open();
if (con.State == ConnectionState.Open)
{
string s = string.Format("INSERT INTO DataTable1 (Username, Password, FName, LName, Bdate, Sex, City, Mail) VALUES ('{0}', '{1}', '{2}', '{3}', #{4}#, {5}, {6}, '{7}')", uname, pass, fname, lname, bd, sex, city, mail);
OleDbCommand cmd = DAL.GetCommand(con, s);
int check = cmd.ExecuteNonQuery();
if (check == 0)
{
con.Close();
Response.Redirect("Reg.aspx?err=-An error has occured. Please try again-");
}
Thank you. 谢谢。
Probably you have some quotes in the passed text, try using parameters ... 可能您在传递的文本中有一些引号,请尝试使用参数...
string s = "INSERT INTO DataTable1 (Username) VALUES (@user)";
OleDbCommand cmd = DAL.GetCommand(con, s);
//Add the parameter ...
OleDbParameter nam = new OleDbParameter("@user",uname);
cmd.Parameters.Add(nam);
Check out OleDbCommand.Parameters if you haven't already. 如果尚未查看OleDbCommand.Parameters 。 It is safer (along the lines of @Mark B's comment), and it will probably clear up the syntax error you have. 它更安全(沿@Mark B的注释行),并且可能会清除您的语法错误。
If you insist on a pure String.Format
approach, simply output cmd.CommandText
for debugging to see where the syntax error lies. 如果您坚持使用纯String.Format
方法,只需输出cmd.CommandText
进行调试即可查看语法错误所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.