2014-12-06 37 views
0

我的应用程序应该将来自职员的简单数据输入到数据库中。日期输入数据库为全零,但应用程序显示正确的日期?

但是,尽管如此,该日期不正确输入,我不明白为什么。

在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; 
    } 
} 

回答

0

Mysql的时间格式为yyyy-mm-dd,你是把它作为一个dd/mm/yyyy这可能是给你的麻烦尝试改变时间格式。

+0

刚试过,没有奏效。 – user2962806 2014-12-06 07:45:49

0

的问题是,在行:

  DateTime saleTime = saleDatePicker.Value; 
     string date = saleTime.ToString("MM/dd/yyyy"); 

(如@ ali786节目)日期MySQL的格式是YYYY-MM-DD。

的代码更正行:

string date = saleTime.ToString("yyyy-MM-dd");