Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.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_Datetime_Casting - Fatal编程技术网

C# 指定的强制转换无效

C# 指定的强制转换无效,c#,asp.net,datetime,casting,C#,Asp.net,Datetime,Casting,我有一个应用程序从数据库中获取日期时间值,但它给了我这个错误 指定的强制转换无效 这是密码 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Golden_RoseConnectionString"].ConnectionString); con.Open(); String sel3 = "select Booking_Date from booking where Booking_ID

我有一个应用程序从数据库中获取日期时间值,但它给了我这个错误

指定的强制转换无效

这是密码

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Golden_RoseConnectionString"].ConnectionString);

con.Open();
String sel3 = "select Booking_Date from booking where Booking_ID='" + Booking_Id + "'";
SqlCommand cmd3 = new SqlCommand(sel2, con);
SqlDataReader n3 = cmd3.ExecuteReader();
n3.Read();
DateTime Booking_Date = (DateTime)n3.GetSqlDateTime(0);
con.Close();

如何解决此问题?

sql日期不是.Net
DateTime
对象

GetSqlDateTime
不进行转换-


它必须转换-

sql日期不是.Net
DateTime
对象

GetSqlDateTime
不进行转换-

必须对其进行转换-

您应该使用:

var Date = Convert.ToDateTime(n3["YOUR_DATE_COLUMN"]);
你应使用:

var Date = Convert.ToDateTime(n3["YOUR_DATE_COLUMN"]);

结果是否返回空值?最好将
Booking\u Date
列类型更改为
datetime
或更好的
datetime 2
。结果是否返回空值?最好将
Booking\u Date
列类型更改为
datetime
或更好
DateTime 2
。虽然不是,但应该可以将其强制转换为
DateTime
,不是吗?()不是,但应该可以将其强制转换为
日期时间
,不是吗?()