我的應用程序應該將來自職員的簡單數據輸入到數據庫中。日期輸入數據庫爲全零,但應用程序顯示正確的日期?
但是,儘管如此,該日期不正確輸入,我不明白爲什麼。
在MessageBox顯示日期的對象的值。
我的表如下:
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
)
是否有它的問題,導致該日期要到全零?我得到的是這樣的數據(從DB拷貝)
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;
}
}
剛試過,沒有奏效。 – user2962806 2014-12-06 07:45:49