日期以全零形式输入数据库,但应用程序显示正确的日期
本文关键字:日期 应用程序 显示 数据库 输入 | 更新日期: 2023-09-27 18:33:28
我的应用程序应该将来自职员的简单数据输入数据库,它确实这样做了。
也就是说,但是,日期输入不正确,我看不出原因。
消息框显示"日期对象"值。
我的表格如下:
CREATE TABLE `SALES` (
`salesDate` date NOT NULL,
`taxable` decimal(10,2) DEFAULT NULL,
`untaxable` decimal(10,2) DEFAULT NULL,
`tax` decimal(10,2) DEFAULT NULL,
`customerName` varchar(52) DEFAULT NULL,
`customerPhone` varchar(12) DEFAULT NULL,
`warranty` int(11) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL
)
是否存在导致日期全部为零的问题?我得到的只是这样的数据(从数据库复制(
INSERT INTO `SALES` VALUES('0000-00-00', 65.00, 65.00, 3.90, '9984', '987987', 987987, '987987');
在应用程序端,提交按钮的事件处理程序如下所示:
private void btn_Submit_Click(object sender, EventArgs e)
{
if (isValidData() && isPresent(rTxt_Description, "Description"))
{
//CONVERT FORM VALUES AND STORE IN VARIABLES TO SEND TO MYSQL QUERY
DateTime saleTime = saleDatePicker.Value;
string date = saleTime.ToString("MM/dd/yyyy");
MessageBox.Show(date, date);
Decimal untaxable = Convert.ToDecimal(txt_Untaxable.Text);
Decimal taxable = Convert.ToDecimal(txt_Taxable.Text);
Decimal tax = Convert.ToDecimal(txt_Tax.Text);
string customerName = txt_CustomerName.Text;
string customerPhone = txt_CustomerPhone.Text;
string description = rTxt_Description.Text;
// Create the query using parameter placeholders, not the actual stringized values....
string query = "INSERT into SALES VALUES (@stime, @taxable, @untaxable, @tax, @cname, @cphone, @warranty, @desc)";
// Create a list of parameters with the actual values with the placeholders names
// Pay attention to the Size value for string parameters, you need to change it
// accordingly to your fields size on the database table.
List<MySqlParameter> prms = new List<MySqlParameter>()
{
new MySqlParameter {ParameterName="@stime", MySqlDbType=MySqlDbType.Date, Value = date },
new MySqlParameter {ParameterName="@taxable", MySqlDbType=MySqlDbType.Decimal, Value = taxable },
new MySqlParameter {ParameterName="@untaxable", MySqlDbType=MySqlDbType.Decimal, Value = untaxable },
new MySqlParameter {ParameterName="@tax", MySqlDbType=MySqlDbType.Decimal, Value = tax },
new MySqlParameter {ParameterName="@cname", MySqlDbType=MySqlDbType.VarChar, Value = customerName, Size = 150 },
new MySqlParameter {ParameterName="@cphone", MySqlDbType=MySqlDbType.VarChar, Value = customerPhone , Size = 150 },
new MySqlParameter {ParameterName="@warranty", MySqlDbType=MySqlDbType.Int32, Value = customerPhone , Size = 150 },
new MySqlParameter {ParameterName="@desc", MySqlDbType=MySqlDbType.VarChar, Value = description , Size = 950 }
};
// Pass query and parameters to the insertion method.
insertValues(query, prms);
连接是这样建立的:
private void insertValues(string q, List<MySqlParameter> parameters)
{
using (MySqlConnection con = new MySqlConnection("My Connection String"))
using (MySqlCommand cmd = new MySqlCommand(q, con))
{
con.Open();
cmd.Parameters.AddRange(parameters.ToArray());
int rowsInserted = cmd.ExecuteNonQuery();
//return rowsInserted;
}
}
Mysql 时间格式yyyy-mm-dd
并且您将其存储为dd/mm/yyyy
这可能会给您尝试更改时间格式带来麻烦。
问题是在以下行中:
DateTime saleTime = saleDatePicker.Value;
string date = saleTime.ToString("MM/dd/yyyy");
(如@ali786所示(日期的 mysql 格式是 yyyy-mm-dd。
更正的代码行:
string date = saleTime.ToString("yyyy-MM-dd");