【问题标题】:Insert Date ( DatePicker ) to SQL using C#使用 C# 将日期 ( DatePicker ) 插入 SQL
【发布时间】:2012-06-07 15:08:01
【问题描述】:

//嗨!每当我尝试使用 DatePicker 插入日期时都会遇到问题

       DateTime birth_date=Convert.ToDateTime(datePickerEBirthDate.SelectedDate);
            SqlConnection SQLconnection = new SqlConnection(@"Server=MyMachineName\SQLEXPRESS;Database=MyDataBase;Integrated Security=SSPI");
            SqlCommand command = SQLconnection.CreateCommand();
            SQLconnection.Open();
            command.CommandText = "INSERT INTO courses " +
            "(name, birthdate) VALUES " +
            "(@name, @birthdate)";
            command.Parameters.AddWithValue("@name", txtName.Text);
            command.Parameters.AddWithValue("@birthdate", birth_date);
            command.ExecuteNonQuery();
        SQLconnection.Close();

//这是错误信息

【问题讨论】:

标签: c#


【解决方案1】:

就像您获取 TextBox.Text 属性 (txtName.Text) 一样,您应该获取 DatePicker.SelectedDate 属性 (dpSBirthDate.SelectedDate)。相反,您出于某种原因尝试转换 DataContext?此外,你可以摆脱 Convert.ToDateTime(...) 调用,因为dpSBirthDate.SelectedDate 将返回一个 DateTime 对象。


更新(这里是一个完全修复的代码部分):

var name = txtName.Text;
var birthdate = dpSBirthDate.SelectedDate;

using (var connection = new SqlConnection(@"Server=MyMachineName\SQLEXPRESS;Database=MyDataBase;Integrated Security=SSPI")
using (var command = connection.CreateCommand())
{
    command.CommandText = "INSERT INTO courses " +
                          "(name, birthdate) VALUES " +
                          "(@name, @birthdate)";

    if (name == null)
        throw new Exception("Name cannot be null.");
    if (!birthdate.HasValue)
        throw new Exception("Birthdate must contain a value.");

    command.Parameters.AddWithValue("@name", name);
    command.Parameters.AddWithValue("@birthdate", birthdate.Value);
    connection.Open();
    command.ExecuteNonQuery();
}

现在,如果此代码失败,它会告诉您原因……您的姓名或出生日期均无效。如果生日仍然无效,那么在执行此代码之前,您必须检查 dpSBirthDate(DatePicker)是否设置为正确的 DateTime。希望对您有所帮助。

还请注意,我通过添加 using 语句、异常处理和尽可能晚地打开连接来清理代码。

【讨论】:

  • 我试过 DateTime 生日 = Convert.ToDateTime( datePickerEBirthDate.SelectedDate);
  • 它给出了相同的输出 {1/1/0001 12:00:00 AM}
  • 虽然它不应该有所作为,但还是去掉 Convert.ToDateTime。这是不必要的。
  • 我不能给出一个错误:错误不能隐式转换类型'System.DateTime?'到“系统。日期时间”。存在显式转换(您是否缺少演员表?)
  • @yasser:我更新了我的答案,以帮助您更好地调试情况和更清晰的代码。不过,我觉得问题在于您在执行此代码之前没有在 DatePicker 中设置日期。否则,没有理由失败。
【解决方案2】:

.NET 中的 DateTime 可以存储从 0001 年 1 月 1 日到 9999 年 12 月 31 日的日期。在 SQL 中,1753 年 1 月 1 日 - 9999 年 12 月 31 日。因此,0001 年 1 月 1 日到 1753 年 1 月 1 日之间的日期不能保存到数据库中。所以,检查解析的birth_date。可能你必须用正确的文化解析日期吗?

【讨论】:

    【解决方案3】:

    您应该使用 datePickerEBirthDate.Value 它将为您提供 datetime 类型,因此无需转换为 DateTime 但您最终会得到时间并且可能不想按此搜索,但这是您的偏好。

    【讨论】:

    • 我试过了,但它给出了运行时错误:可空对象必须有一个值。
    • 好吧,您使用的是 WPF,在这种情况下使用 datePickerEBirthDate.SelectedDate 是不同的,但您必须确保它有一个开始日期,您可以通过在window_loaded 事件,即 datePickerEBirthDate.SelectedDate = DateTime.Now; SelectedDate 是一个可以为空的值类型,这意味着它不保存该值,因此您可以使用它通过 if(datePickerEBirthDate.SelectedDate != null) { DateTimebirth_date = datePickerEBirthDate.SelectedDate.Value;} 首先查看它是否不为空帮助:)
    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2013-06-10
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多