0

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

1 回答 1

0

这会有帮助吗

        string test = "1234";
        string startTime = test.Substring(0, 2);
        string Endtime = test.Substring(test.Length - 2);
        string NewTime = startTime + ":" + Endtime;
于 2012-07-30T09:05:17.370 回答