我遇到了一个问题,我无法将 Excel 开始时间和结束时间保存到数据库中。对于 Excel,时间类型为“String”,但对于数据库,时间类型为“Time”。有什么方法可以将 Excel 时间保存到数据库中。
这是我将数据保存到数据库中的代码,但它只保存名称和日期,但不保存时间。任何帮助将不胜感激。
private void btnSave_Click(object sender, EventArgs e)
{
try
{
foreach (DataRow dr in dt.Rows)
{
string strEmployee = dr["Employee Name"].ToString();
//store data into employeeshift DB
using (satsEntities db = new satsEntities())
{
ufi empShift;
IList<employeeschedule> employeeList = dict[strEmployee];
foreach (employeeschedule es in employeeList)
{
empShift = new ufi();
TimeSpan a = new TimeSpan(Convert.ToInt32(es.startTime.Substring(0,2), Convert.ToInt32(es.startTime.Substring(2,2))));
TimeSpan b = new TimeSpan(Convert.ToInt32(es.endTime.Substring(0,2), Convert.ToInt32(es.endTime.Substring(2,2))));
empShift.UFISDate = es.day;
empShift.EmployeeName = strEmployee;
empShift.startTime = a;
empShift.endTime = b;
db.ufis.AddObject(empShift);
}
db.SaveChanges();
}
}
MessageBox.Show("Data is stored to database successfully.");
}
catch (Exception ex)
{
//showMessage("UNABLE to store to database successfully.");
//show error
}
}
由于我的声誉低,我无法发布自己的答案。毕竟,这是让我的代码正常工作的答案。
private void btnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in dt.Rows)
{
string strEmployee = dr["Employee Name"].ToString();
//store data into employeeshift DB
using (satsEntities db = new satsEntities())
{
ufi empShift;
IList<employeeschedule> employeeList = dict[strEmployee];
foreach (employeeschedule es in employeeList)
{
empShift = new ufi();
TimeSpan a = new TimeSpan(Convert.ToInt32(es.startTime.Substring(0,2)), Convert.ToInt32(es.startTime.Substring(2,2)), 0);
TimeSpan b = new TimeSpan(Convert.ToInt32(es.endTime.Substring(0,2)), Convert.ToInt32(es.endTime.Substring(2,2)), 0);
empShift.UFISDate = es.day;
empShift.EmployeeName = strEmployee;
empShift.startTime = a;
empShift.endTime = b;
db.ufis.AddObject(empShift);
db.SaveChanges();
}
}
}
MessageBox.Show("Data is stored to database successfully.");
}