Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.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向SQL插入日期(日期选择器)#_C# - Fatal编程技术网

C# 使用C向SQL插入日期(日期选择器)#

C# 使用C向SQL插入日期(日期选择器)#,c#,C#,//嗨!每当我尝试使用DatePicker插入日期时,我都会遇到一个问题 DateTime birth_date=Convert.ToDateTime(datePickerEBirthDate.SelectedDate); SqlConnection SQLconnection = new SqlConnection(@"Server=MyMachineName\SQLEXPRESS;Database=MyDataBase;Integrated Securi

//嗨!每当我尝试使用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();
//这是错误信息

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


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

现在,如果这个代码失败,它会告诉你为什么。。。您的姓名或出生日期无效。如果生日仍然无效,则在执行此代码之前,必须检查dpSBirthDate(日期选择器)是否设置为正确的日期时间。希望有帮助


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

DateTime in.NET可以存储从0001年1月1日到9999年12月31日的日期。在SQL 1753年1月1日至9999年12月31日期间。所以,0001年1月1日至1753年1月1日之间的日期无法保存到数据库中。所以,检查解析的出生日期。可能您必须使用正确的区域性分析日期

您应该使用DatePickereBhirthDate.Value,该值将为您提供datetime类型,因此无需转换为datetime,但您最终将获得该类型的时间,并且可能不希望按此进行搜索,但这是您的首选项。

使用visual studio断点并调试代码。看看出生日期变量的值是多少?这对你有用吗?[[1]:值出现在出生日期变量{1/1/0001 12:00:00 AM}中。我尝试了DateTime birthdate=Convert.ToDateTime(datepickerebirhdate.SelectedDate);它给出了相同的输出{1/1/0001 12:00:00 AM}尽管这不会有什么区别,但还是要去掉Convert.ToDateTime。这是不必要的。我无法确定它是否会给出错误:错误无法将类型“System.DateTime”隐式转换为“System.DateTime”。存在显式转换(是否缺少转换?)@yasser:我更新了我的答案,以帮助您更好地调试这种情况和更干净的代码。不过,我有一种感觉,问题是您在执行此代码之前没有在日期选择器中设置日期。否则,没有理由失败。我尝试过它,但它给出了一个运行时错误:Nullable对象必须有一个值。好的您使用的WPF与本例中使用的DatePickereBirithDate.SelectedDate不同,但您必须确保它有一个开始的日期,您可以通过在加载的窗口事件中设置一个解除日期来解决这个问题,即DatePickereBirithDate.SelectedDate=DateTime。现在,SelectedDate是一个可为空的值类型,这意味着这不需要no保留该值,以便您可以使用它来查看它是否不是空的,前提是(DatePickereBhirthDate.SelectedDate!=null){DateTime birth\u date=DatePickereBhirthDate.SelectedDate.value;}希望这有帮助:)
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();
}