C# 需要帮助将标签的文本转换为 Access 数据库的日期时间
C# Need help in converting Label's text to Date Time for Access Database
我已经在此处和其他站点查看了其他问题,但似乎无法解决此问题。
我正在尝试 UPDATE
我的 Access 数据库中的现有数据。
我有一个按钮可以做到这一点。我的标识符之一是标签的文本(该标签的值来自 datagrid_cellcontentclick
)。因此,在我可以 UPDATE
我的数据库之前,我需要先将该标签转换为 DateTime
格式,对吗?这就是我所做的/按钮中的代码:
string dateee = label_todatetime.Text;
string formatuu = "dd/MM/yyyy HH:mm:ss";
//DateTime pleaseconvert = DateTime.ParseExact(dateee, "G", null); //I did try this... tried if the "G" will do the trick because it means "General Date" which is the same format as my Access Database.. But still didn't work..
DateTime pleaseconvert = DateTime.ParseExact(dateee, formatuu, CultureInfo.InvariantCulture);
//MessageBox.Show(pleaseconvert.ToString()); //I just want to see if its outputing the correct format..
try
{
accessconnection.Open();
string mahquery = "UPDATE Account SET" +
"[UName] = '" + textBox4.Text + "', [Password] = '" + textBox5.Text + "'" +
" WHERE " +
"[FName] = '" + textBox1.Text + "' and [MName] = '" + textBox2.Text + "' and [LName] = '" + textBox3.Text + "' and [Added At] = '" + pleaseconvert + "'";
OleDbCommand accesscommand = new OleDbCommand(mahquery, accessconnection);
accesscommand.ExecuteNonQuery();
accessconnection.Close();
我的 Access 数据库中 Added At
的格式是 General Date
,它的值是这样的:27/04/2019 14:01:23
(并且标签可以得到相同的格式吗?我只需要转换String
到 DateTime
?)... 这就是为什么我的 formatuu
是 "dd/MM/yyyy HH:mm:ss"
... 但是每当我点击按钮时它总是说
Data type mismatch in criteria expression
感谢亚历山大·彼得罗夫 answer。这是 updated/working 代码:
//string dateee = label_todatetime.Text;
//string formatuu = "#dd/MM/yyyy HH:mm:ss#";
////DateTime pleaseconvert = DateTime.ParseExact(dateee, "G", null);
//DateTime pleaseconvert = DateTime.ParseExact(dateee, formatuu, CultureInfo.InvariantCulture);
//MessageBox.Show(pleaseconvert.ToString());
try
{
accessconnection.Open();
string mahquery = "UPDATE Account SET" +
"[UName] = @uname, [Password] = @passw" +
" WHERE " +
"[FName] = @fname and [MName] = @mname and [LName] = @lname and [Added At] = @dateadded";
OleDbCommand accesscommand = new OleDbCommand(mahquery, accessconnection);
//accesscommand.Parameters.Add("@uname", textBox4.Text);
//accesscommand.Parameters.Add("@password", textBox5.Text);
//accesscommand.Parameters.Add("@fname", textBox1.Text);
//accesscommand.Parameters.Add("@mname", textBox2.Text);
//accesscommand.Parameters.Add("@lname", textBox3.Text);
//accesscommand.Parameters.Add("@dateadded", label_todatetime.Text);
accesscommand.Parameters.Add("@uname", SqlDbType.NVarChar).Value = textBox4.Text;
accesscommand.Parameters.Add("@passw", SqlDbType.NVarChar).Value = textBox5.Text;
accesscommand.Parameters.Add("@fname", SqlDbType.NVarChar).Value = textBox1.Text;
accesscommand.Parameters.Add("@mname", SqlDbType.NVarChar).Value = textBox2.Text;
accesscommand.Parameters.Add("@lname", SqlDbType.NVarChar).Value = textBox3.Text;
accesscommand.Parameters.Add("dateadded", SqlDbType.DateTime).Value = label_todatetime.Text;
accesscommand.ExecuteNonQuery();
accessconnection.Close();
MessageBox.Show("Successfuly updated into the database");
如您所见,代码更短(如果我删除注释)并且不需要 DateTime.ParseExact
将 String
转换为 DateTime
数据类型。刚用过:
cmd.Parameters.Add("String Parameter Name HERE", SqlDbType."Data Type HERE").Value = "Variable HERE like Texbox1.Text"
非常感谢!!
我已经在此处和其他站点查看了其他问题,但似乎无法解决此问题。
我正在尝试 UPDATE
我的 Access 数据库中的现有数据。
我有一个按钮可以做到这一点。我的标识符之一是标签的文本(该标签的值来自 datagrid_cellcontentclick
)。因此,在我可以 UPDATE
我的数据库之前,我需要先将该标签转换为 DateTime
格式,对吗?这就是我所做的/按钮中的代码:
string dateee = label_todatetime.Text;
string formatuu = "dd/MM/yyyy HH:mm:ss";
//DateTime pleaseconvert = DateTime.ParseExact(dateee, "G", null); //I did try this... tried if the "G" will do the trick because it means "General Date" which is the same format as my Access Database.. But still didn't work..
DateTime pleaseconvert = DateTime.ParseExact(dateee, formatuu, CultureInfo.InvariantCulture);
//MessageBox.Show(pleaseconvert.ToString()); //I just want to see if its outputing the correct format..
try
{
accessconnection.Open();
string mahquery = "UPDATE Account SET" +
"[UName] = '" + textBox4.Text + "', [Password] = '" + textBox5.Text + "'" +
" WHERE " +
"[FName] = '" + textBox1.Text + "' and [MName] = '" + textBox2.Text + "' and [LName] = '" + textBox3.Text + "' and [Added At] = '" + pleaseconvert + "'";
OleDbCommand accesscommand = new OleDbCommand(mahquery, accessconnection);
accesscommand.ExecuteNonQuery();
accessconnection.Close();
我的 Access 数据库中 Added At
的格式是 General Date
,它的值是这样的:27/04/2019 14:01:23
(并且标签可以得到相同的格式吗?我只需要转换String
到 DateTime
?)... 这就是为什么我的 formatuu
是 "dd/MM/yyyy HH:mm:ss"
... 但是每当我点击按钮时它总是说
Data type mismatch in criteria expression
感谢亚历山大·彼得罗夫 answer。这是 updated/working 代码:
//string dateee = label_todatetime.Text;
//string formatuu = "#dd/MM/yyyy HH:mm:ss#";
////DateTime pleaseconvert = DateTime.ParseExact(dateee, "G", null);
//DateTime pleaseconvert = DateTime.ParseExact(dateee, formatuu, CultureInfo.InvariantCulture);
//MessageBox.Show(pleaseconvert.ToString());
try
{
accessconnection.Open();
string mahquery = "UPDATE Account SET" +
"[UName] = @uname, [Password] = @passw" +
" WHERE " +
"[FName] = @fname and [MName] = @mname and [LName] = @lname and [Added At] = @dateadded";
OleDbCommand accesscommand = new OleDbCommand(mahquery, accessconnection);
//accesscommand.Parameters.Add("@uname", textBox4.Text);
//accesscommand.Parameters.Add("@password", textBox5.Text);
//accesscommand.Parameters.Add("@fname", textBox1.Text);
//accesscommand.Parameters.Add("@mname", textBox2.Text);
//accesscommand.Parameters.Add("@lname", textBox3.Text);
//accesscommand.Parameters.Add("@dateadded", label_todatetime.Text);
accesscommand.Parameters.Add("@uname", SqlDbType.NVarChar).Value = textBox4.Text;
accesscommand.Parameters.Add("@passw", SqlDbType.NVarChar).Value = textBox5.Text;
accesscommand.Parameters.Add("@fname", SqlDbType.NVarChar).Value = textBox1.Text;
accesscommand.Parameters.Add("@mname", SqlDbType.NVarChar).Value = textBox2.Text;
accesscommand.Parameters.Add("@lname", SqlDbType.NVarChar).Value = textBox3.Text;
accesscommand.Parameters.Add("dateadded", SqlDbType.DateTime).Value = label_todatetime.Text;
accesscommand.ExecuteNonQuery();
accessconnection.Close();
MessageBox.Show("Successfuly updated into the database");
如您所见,代码更短(如果我删除注释)并且不需要 DateTime.ParseExact
将 String
转换为 DateTime
数据类型。刚用过:
cmd.Parameters.Add("String Parameter Name HERE", SqlDbType."Data Type HERE").Value = "Variable HERE like Texbox1.Text"
非常感谢!!