C#:DateTimePicker 的值 属性 与 MS Access 数据库 2013 的 Date/Time 不兼容
C#: Value property of DateTimePicker not compatible with Date/Time of MS Access database 2013
我正在尝试实现一个时间戳,该时间戳将进入 ms access 2013 table,在使用 .NET 框架和 visual studio 2013 的 C# 应用程序中,以记录谁拥有在我的应用程序中登录,但我得到一个错误,我的 INSERT INTO 语句有语法问题,但是当我一起删除时间戳参数并停止使用 datetimepicker 时,该应用程序将设法在数据库中插入用户名和密码成功了,所以我认为 ms access 2013 的 Date/Time 字段与 visual studio 框架内的 datetimepicker 工具的值之间存在兼容性问题。那么现在,我问你,获取当前正确日期和时间并将其插入所述数据库的最佳方法是什么?我已经尝试将字段设置为短文本并设置日期时间.ToString 的现在 属性,这也不起作用。
private void loginButton_Click(object sender, EventArgs e)
{
using (OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["LibrarieConectare"].ConnectionString))
{
OleDbCommand command = connect.CreateCommand();
OleDbCommand _logins = connect.CreateCommand();
dateTimePickerBirthAngajat.Value = DateTime.Now;
command.Parameters.AddWithValue("@user", textBoxAccount.Text);
command.Parameters.AddWithValue("@pass", textBoxParola.Text);
_logins.Parameters.AddWithValue("@utilizator", textBoxAccount.Text);
_logins.Parameters.AddWithValue("@parola", textBoxParola.Text);
_logins.Parameters.AddWithValue("@datetime", dateTimePickerBirthAngajat.Value);
command.CommandText = "SELECT IDAngajat FROM Angajati WHERE NumeUtilizator=@user AND Parola=@pass";
_logins.CommandText = "INSERT INTO Logins (Utilizator, Parola, TimeStamp) VALUES (@utilizator, @parola, @datetime)";
connect.Open();
try
{
using (OleDbDataReader dr = command.ExecuteReader())
{
if (dr.HasRows)
{
this.DialogResult = DialogResult.OK;
user = textBoxAccount.Text;
parola = textBoxParola.Text;
if (rememberMe.Checked == true)
{
_logins.ExecuteNonQuery();
}
}
else
{
this.DialogResult = DialogResult.Cancel;
MessageBox.Show("Login failed!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Eroare baza de date: " + ex);
}
finally
{
this.Close();
}
}
}
尝试将您的 Date 显式解析为 DateTime
对象,而不是通过 DateTime.Parse()
方法传入字符串:
// Parse your date as an actual Date
DateTime date = DateTime.Parse(dateTimePickerBirthAngajat.Value);
// Pass that parameter along to be executed
_logins.Parameters.AddWithValue("@datetime", date);
根据日期的格式设置,您实际上可能需要使用 DateTime.ParseExact()
方法,该方法允许您提供格式字符串(例如 "MM/dd/yyyy"、"yyyy-MM-dd"、等) 以指示应如何解析您的字符串。
我正在尝试实现一个时间戳,该时间戳将进入 ms access 2013 table,在使用 .NET 框架和 visual studio 2013 的 C# 应用程序中,以记录谁拥有在我的应用程序中登录,但我得到一个错误,我的 INSERT INTO 语句有语法问题,但是当我一起删除时间戳参数并停止使用 datetimepicker 时,该应用程序将设法在数据库中插入用户名和密码成功了,所以我认为 ms access 2013 的 Date/Time 字段与 visual studio 框架内的 datetimepicker 工具的值之间存在兼容性问题。那么现在,我问你,获取当前正确日期和时间并将其插入所述数据库的最佳方法是什么?我已经尝试将字段设置为短文本并设置日期时间.ToString 的现在 属性,这也不起作用。
private void loginButton_Click(object sender, EventArgs e)
{
using (OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["LibrarieConectare"].ConnectionString))
{
OleDbCommand command = connect.CreateCommand();
OleDbCommand _logins = connect.CreateCommand();
dateTimePickerBirthAngajat.Value = DateTime.Now;
command.Parameters.AddWithValue("@user", textBoxAccount.Text);
command.Parameters.AddWithValue("@pass", textBoxParola.Text);
_logins.Parameters.AddWithValue("@utilizator", textBoxAccount.Text);
_logins.Parameters.AddWithValue("@parola", textBoxParola.Text);
_logins.Parameters.AddWithValue("@datetime", dateTimePickerBirthAngajat.Value);
command.CommandText = "SELECT IDAngajat FROM Angajati WHERE NumeUtilizator=@user AND Parola=@pass";
_logins.CommandText = "INSERT INTO Logins (Utilizator, Parola, TimeStamp) VALUES (@utilizator, @parola, @datetime)";
connect.Open();
try
{
using (OleDbDataReader dr = command.ExecuteReader())
{
if (dr.HasRows)
{
this.DialogResult = DialogResult.OK;
user = textBoxAccount.Text;
parola = textBoxParola.Text;
if (rememberMe.Checked == true)
{
_logins.ExecuteNonQuery();
}
}
else
{
this.DialogResult = DialogResult.Cancel;
MessageBox.Show("Login failed!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Eroare baza de date: " + ex);
}
finally
{
this.Close();
}
}
}
尝试将您的 Date 显式解析为 DateTime
对象,而不是通过 DateTime.Parse()
方法传入字符串:
// Parse your date as an actual Date
DateTime date = DateTime.Parse(dateTimePickerBirthAngajat.Value);
// Pass that parameter along to be executed
_logins.Parameters.AddWithValue("@datetime", date);
根据日期的格式设置,您实际上可能需要使用 DateTime.ParseExact()
方法,该方法允许您提供格式字符串(例如 "MM/dd/yyyy"、"yyyy-MM-dd"、等) 以指示应如何解析您的字符串。