0
我遇到了一个问题,我无法将Excel的开始时间和结束时间保存到数据库中。对于Excel,时间类型为“字符串”,但对于数据库,时间类型为“时间”。有什么方法可以将Excel时间保存到数据库中。Excel(字符串)到数据库(TimeSpan)
这是我的代码,它将数据保存到数据库中,但它只保存名称和日期,但不保存时间。任何帮助将不胜感激。
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.");
}
你没有做与'了'什么或'B' ... – 2012-07-30 07:01:56
嗯是啊,我现在已经做到了,我已经再次更新我的代码,因为它是即使我使用了a和b,仍然无法工作。 – rookie 2012-07-30 07:13:56