gpt4 book ai didi

c# - 输入数据库的日期全为零,但应用程序显示正确的日期?

转载 作者:行者123 更新时间:2023-11-29 23:20:46 25 4
gpt4 key购买 nike

我的应用程序应该将职员的简单数据输入数据库,它确实这样做了。

但是,日期输入不正确,我不明白为什么。

消息框显示日期对象值。

我的表格如下:

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 这可能会给您带来尝试更改时间格式的麻烦。

关于c# - 输入数据库的日期全为零,但应用程序显示正确的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com