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