嘿,我的插入语句不起作用,我使用相同的代码将其他面板数据插入到excel工作表中,在这里工作正常,但是当我尝试使用第二个面板在其他工作表中插入数据时,会抛出异常“插入INTO语句无效”,我在其中检查了每件事,但找不到任何错误。我正在使用OleDb进行插入。 这是我第一次插入面板时使用的相同代码。
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
String filename1 = @"E:DB\TestDB.xlsx";
String connection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename1 + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
OleDbConnection con = new OleDbConnection(connection);
con.Open();
int id = 4;
string user = txtMUserName.Text.ToString();
string pass = txtMPassword.Text.ToString();
string role = txtMRole.Text.ToString();
DateTime date = DateTime.Now;
string Date = date.ToString("dd/MM/yyyy");
//string Time = date.ToLongTimeString();
string Time = "3:00 AM";
String Command = "Insert into [Test$] (UserID, UserName, Password, Role, Created_Date,Created_Time) VALUES ('"
+ id.ToString() + "','"
+ user + "','"
+ pass + "','"
+ role + "','"
+ Date + "','"
+ Time + "')";
OleDbCommand cmd = new OleDbCommand(Command, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Success!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
答案 0 :(得分:1)
似乎您正在为列Password
使用保留名称。您需要使用[]
对其进行转义:
string Command = "Insert into [Test$] (UserID, UserName, [Password], Role, Created_Date,Created_Time) VALUES ('"
+ id.ToString() + "','"
+ user + "','"
+ pass + "','"
+ role + "','"
+ Date + "','"
+ Time + "')";