我正在尝试将用户名和 DateTime 对象保存到 MS-Access 数据库,但出现“INSERT INTO 语句中的语法错误”。当我调试程序时,我看到日期已正确传递。此外,我完全删除了 DateTime 信息,并仅使用用户名使用 insert 命令并正确保存,因此它与 DateTime 本身有关,但我不知道是什么。以下是相关代码:
//event handler for Begin Program button
private void btnBeginProgram_Click(object sender, EventArgs e)
{
//assign DateTime object to current computer date/time
busObject.DtDate = DateTime.Now;
//assign input to busObject.UserName property
busObject.UserName = txtEnterName.Text;
//call method to save input data
busObject.SaveData();
this.Close();
}
//properties for variables and objects
public string UserName
{
get { return userName; }
set { userName = value; }
}
public DateTime DtDate
{
get { return dtDate; }
set { dtDate = value; }
}
//method to call SaveData method in ProgramLoginDAL class
public void SaveData()
{
ProgramLoginDAL.SaveData(this);
}
//method to save user input to database
public static void SaveData(ProgramLoginBOL busObject)
{
try
{
OleDbCommand cmd = aConnection.CreateCommand();
String sSQLCommand = "INSERT INTO ProgramLogin (UserName, DateTime) VALUES (?,?)";
cmd.Parameters.AddWithValue("?", busObject.UserName);
cmd.Parameters.AddWithValue("?", busObject.DtDate);
if (aConnection.State == ConnectionState.Closed)
{
aConnection.Open();
}
cmd.CommandText = sSQLCommand;
// Execute the SQL command
cmd.ExecuteNonQuery();
aConnection.Close();
MessageBox.Show("Data Saved");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
MessageBox.Show("Error! Data was not saved.");
}
}