本文介绍了c#DateTime和T-SQL DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将t-sql datetime转换为c#datetime?
我总是遇到InvalidCastException。
代码员工:
How i can convert t-sql datetime to c# datetime?
I always got InvalidCastException.
code Employee:
public Employee(string fName, string lName, int age, int earnings, DateTime dateOfRegistration)
{
this.First_Name = fName;
this.Last_Name = lName;
this.Age = age;
this.Earnings = earnings;
this.DateOfRegistration = dateOfRegistration;
}
代码插入数据库:它运作良好!
code Insert To Data Base: It work good!
string command = string.Format("Insert into Employees Values ('{0}', '{1}', '{2}', '{3}', '{4}')",
emp.First_Name, emp.Last_Name, emp.Age, emp.Earnings, emp.DateOfRegistration.Date);
代码从数据库到新员工:有InvalidCastException:
code From data base to new employee: there is InvalidCastException:
while(dr.Read())
{
emps.Add(new Employee((string)dr["First_Name"], (string)dr["Last_Name"], (int)dr["Age"], (int)dr["Earnings"],Convert.ToDateTime(dr["Date_of_registration"].ToString())));
}
推荐答案
emps.Add(new Employee((string)dr["First_Name"], (string)dr["Last_Name"], (int)dr["Age"], (int)dr["Earnings"],(DateTime)dr["Date_of_registration"]));
如果不是'然后你需要查看数据库中存储的内容,因为存在一个主要问题!
If it isn't then you need to look at what is stored in your DB, as there is a major problem!
这篇关于c#DateTime和T-SQL DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!