Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将所选日期保存到数据库?_C#_Asp.net_Sql Server_Date_Gridview - Fatal编程技术网

C# 如何将所选日期保存到数据库?

C# 如何将所选日期保存到数据库?,c#,asp.net,sql-server,date,gridview,C#,Asp.net,Sql Server,Date,Gridview,我有一个日历,用户将选择日期,它必须存储到数据库中。请帮我把日期保存到数据库 数据库中的日期数据类型为“日期” “输入字符串格式不正确”的日期出现错误 我试图从日历中插入的日期为2015年11月4日 这是我的代码 类别代码 public Boolean InsertBookings(int BookingID, DateTime Date, DateTime Returndate, int ReturntimeID, string Returnpickupaddress, string Retu

我有一个日历,用户将选择日期,它必须存储到数据库中。请帮我把日期保存到数据库

数据库中的日期数据类型为“日期”

“输入字符串格式不正确”的日期出现错误


我试图从日历中插入的日期为2015年11月4日

这是我的代码

类别代码

public Boolean InsertBookings(int BookingID, DateTime Date, DateTime Returndate, int ReturntimeID, string Returnpickupaddress, string Returndropofdetails, string Comments)
    {

        OConnection = new SqlConnection(_ConnectionString);
        oCommand.Connection = OConnection;

        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.CommandText = "SP_INSERT_BOOKINGS";
        oCommand.Parameters.AddWithValue("@ParameterID", (int)SQLparameters.Insert);
        oCommand.Parameters.AddWithValue("@BookingID", BookingID);
        oCommand.Parameters.AddWithValue("@Date", Date);
        oCommand.Parameters.AddWithValue("@Returndate", Returndate);
        oCommand.Parameters.AddWithValue("@RetuntimeID", ReturntimeID);
        oCommand.Parameters.AddWithValue("@Returnpickupaddress", Returnpickupaddress);
        oCommand.Parameters.AddWithValue("@Returndropofdetails", Returndropofdetails);
        oCommand.Parameters.AddWithValue("@Comments", Comments);

        OConnection.Open();
        Boolean _Results = Convert.ToBoolean(oCommand.ExecuteNonQuery());
        OConnection.Close();
        return _Results;

    }
日历代码

protected void BookingCalendar1_SelectionChanged(object sender, EventArgs e)
{
    txtDate.Text = BookingCalendar1.SelectedDate.ToString("d");
    BookingCalendar1.Visible = false;
}
保存按钮

protected void btnbookings_Click(object sender, EventArgs e)
        {

            try
            {

                bool _var = _booking.InsertBookings(_BooingID,
                Convert.ToDateTime(txtdate.Text.ToString()),
                Convert.ToDateTime(txtReturndate.Text.ToString()),
                int.Parse(cboReturntime.SelectedValue.ToString()),
                txtReturnpickupaddress.Text,
                txtReturndropofdetails.Text,
                txtComments.Text);
            }
            catch (Exception ex)
            {

            }
        }

您可以尝试将输入数据库的日期值格式化为ISO-8601字符串,例如yyyy-MM-dd


对于DateTime对象,您可以使用Date.UtcNow.ToString(“o”)

您可以将日期列数据类型更改为varchar(4)以消除此错误,但这并不能解决您的问题。为了得到准确的答案,请在表格中插入一个样本日期

将这两种数据类型都设置为
DATETIME
。在插入查询的数据库中使用
CONVERT(DATE,@YourDateTime)
如果需要,我只需要DATE而不需要time日期我正试图从日历2015年11月4日插入的日期,对吧,但它是DATETIME对象?它代表了你想输入的正确日期?因此,您可以更改其输出方式…如果我理解正确,我已将此格式更改为“MM/dd/yyyy”,但仍然没有结果..请提供帮助,但在将日期发送到数据库时,格式应为“yyyy-MM-dd”,例如oCommand.Parameters.AddWithValue(@date),date.ToString(“yyyy-MM-dd”);我使用过程插入意味着,将datetime转换为Date,然后插入,我仍然面临着值为1的相同问题。我确实指定了DataTextField和DataVlaue fieldopen新线程,并将您的代码发送到那里以解决它(与我共享其链接)。如果您的日期问题已解决,请标记答案。