0

如果日期早于 12 日,则以格式在文本框中输入日期时,DD/MM/YYYY它将存储为MM/DD/YYYY. 我需要将其存储为类型。

protected void InsertButton_Click(object sender, EventArgs e)
{

    Int32 claim = (Int32)Session["Claim"];
    string claim1 = Convert.ToString(claim);
    Label4.Text = "Claim Number : " + (String)claim1;
    Label4.Visible = true;

    // declare variables to hold info enterend by user
    string expenseType;
    string purpose;
    decimal amount;
    DateTime date;
    Boolean receipts;

    //assign info entered to variables entered above
    expenseType = DropDownList1. Text;
    purpose = PurposeofExpenseTextBox.Text;
    amount = Convert.ToDecimal(AmountSpentTextBox.Text);
    date = Convert.ToDateTime(DatePurchasedTextBox.Text);
    receipts = CheckBox1.Checked;
    DateTime today = DateTime.Today;

    // create connection string "conn"
    string mapPath = Server.MapPath(".");
    string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
       @"Data source= "*****";
    OleDbConnection conn = new OleDbConnection(ConnectionString);



    try
    {
        //create query to insert values into Expenses table   
        OleDbCommand cmd = new OleDbCommand("INSERT INTO Expenses (ExpenseType, PurposeofExpense, AmountSpent, DatePurchased, strEmpName, Receipts, EmployeeID, ExpNumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", conn);
        cmd.Parameters.Add("@ExpenseType", OleDbType.VarChar).Value = DropDownList1.Text;
        cmd.Parameters.Add("@PurposeofExpense", OleDbType.VarChar).Value = PurposeofExpenseTextBox.Text;
        cmd.Parameters.Add("@AmountSpent", OleDbType.VarChar).Value = AmountSpentTextBox.Text;
        cmd.Parameters.Add("@DatePurchased", OleDbType.VarChar).Value = DatePurchasedTextBox.Text;
        cmd.Parameters.Add("@strEmpName", OleDbType.VarChar).Value = (String)Session["New"];
        cmd.Parameters.Add("@Receipts", OleDbType.Boolean).Value = CheckBox1.Checked;
        cmd.Parameters.Add("@EmployeeID", OleDbType.VarChar).Value = (String)Session["Number"];
        cmd.Parameters.Add("@ExpNumber", OleDbType.VarChar).Value = (Int32)Session["Claim"];


        //open connection "conn"
        conn.Open();
        //execute insert query
        cmd.ExecuteNonQuery();
4

1 回答 1

2

来自MSDN

日期值必须根据 ODBC 规范日期格式分隔或由日期时间分隔符 ("#") 分隔。否则,Microsoft Access 会将值视为算术表达式,并且不会引发警告或错误。例如,日期“1996 年 3 月 5 日”必须表示为 {d '1996-03-05'} 或 #03/05/1996#;否则,如果仅提交 03/05/1993,Microsoft Access 会将其计算为 3 除以 5 除以 1996。此值向上舍入为整数 0,并且由于零日映射到 1899-12-31,因此这是使用的日期。管道字符 (|) 不能用于日期值,即使用反引号括起来。

您需要在保存前检查日期格式。使用一些面具。

于 2013-04-02T12:35:01.370 回答