Excel(字符串)到数据库(TimeSpan)

时间:2012-07-30 07:00:09

标签: c# database excel timespan

我遇到了一个问题,我无法将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.");
    }

1 个答案:

答案 0 :(得分:0)

这会有帮助吗

        string test = "1234";
        string startTime = test.Substring(0, 2);
        string Endtime = test.Substring(test.Length - 2);
        string NewTime = startTime + ":" + Endtime;