Skip to main content
 首页 » 编程设计

c#之指定的转换无效

2024年02月05日13think

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

Specified cast is not valid

这是代码

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(); 

我该如何解决这个问题?

请您参考如下方法:

你应该使用:

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