2017-03-06 71 views
0

Sql Server的变量TEST_TIME数据类型为Time(7)
我在C#中创建了表,并且它自动分配了Timespan数据类型。
现在,我试图上传csv文件数据到SQL数据库,它给我一个错误“不能隐式地将DateTime转换为Timespan”。解决这个问题的最好方法是什么? 用户首先选择的CSV文件:无法将DateTime隐式转换为Timespan

private void button8_Click(object sender, EventArgs e) 
     { 
       try 
        { 
         using (OpenFileDialog openfiledialog1 = new OpenFileDialog() 
         {Filter = "Excel Workbook 97-2003|*.xls|Excel Workbook|*.xlsx|Excel Workbook|*.xlsm|Excel Workbook|*.csv|Excel Workbook|*.txt", ValidateNames = true })      
         { 
--After some IFs-- 

    else if (openfiledialog1.FilterIndex == 4) 
           { 

            DataTable oDataTable = null; 
            int RowCount = 0; 
            string[] ColumnNames = null; 
            string[] oStreamDataValues = null; 
            //using while loop read the stream data till end 
            while (!oStreamReader.EndOfStream) 
            { 
             String oStreamRowData = oStreamReader.ReadLine().Trim(); 
             if (oStreamRowData.Length > 0) 
             { 
              oStreamDataValues = oStreamRowData.Split(','); 
              //Bcoz the first row contains column names, we will populate 
              //the column name by 
              //reading the first row and RowCount-0 will be true only once 
              if (RowCount == 0) 
              { 
               RowCount = 1; 
               ColumnNames = oStreamRowData.Split(','); 
               oDataTable = new DataTable(); 

               //using foreach looping through all the column names 
               foreach (string csvcolumn in ColumnNames) 
               { 
                DataColumn oDataColumn = new DataColumn(csvcolumn.ToUpper(), typeof(string)); 

                //setting the default value of empty.string to newly created column 
                oDataColumn.DefaultValue = string.Empty; 

                //adding the newly created column to the table 
                oDataTable.Columns.Add(oDataColumn); 
               } 
              } 
              else 
              { 
               //creates a new DataRow with the same schema as of the oDataTable    
               DataRow oDataRow = oDataTable.NewRow(); 

               //using foreach looping through all the column names 
               for (int i = 0; i < ColumnNames.Length; i++) 
               { 
                oDataRow[ColumnNames[i]] = oStreamDataValues[i] == null ? string.Empty : oStreamDataValues[i].ToString(); 
               } 

               //adding the newly created row with data to the oDataTable  
               oDataTable.Rows.Add(oDataRow); 
              } 
             } 
            } 
            result.Tables.Add(oDataTable); 
            //close the oStreamReader object 
            oStreamReader.Close(); 
            //release all the resources used by the oStreamReader object 
            oStreamReader.Dispose(); 
            dataGridView5.DataSource = result.Tables[oDataTable.TableName]; 
           } 

这里是代码:

private void button9_Click(object sender, EventArgs e) 
      { 

       try 
       {    


        DataClasses1DataContext conn = new DataClasses1DataContext(); 
     else if (textBox3.Text.Contains("GEN_EX")) 
      { 
       foreach (DataTable dt in result.Tables) 
       { 
        foreach (DataRow dr in dt.Rows) 
        { 
         GEN_EX addtable = new GEN_EX() 
         { 

          EX_ID = Convert.ToByte(dr[0]), 
          DOC_ID = Convert.ToByte(dr[1]), 
          PATIENT_NO = Convert.ToByte(dr[2]), 
          TEST_DATE = Convert.ToDateTime(dr[3]),      
          **TEST_TIME = Convert.ToDateTime((dr[4])),** 

         }; 
         conn.GEN_EXs.InsertOnSubmit(addtable); 
        } 
       } 
       conn.SubmitChanges(); 
       MessageBox.Show("File uploaded successfully"); 
      } 
    else 
     { 
      MessageBox.Show("I guess table is not coded yet"); 
     } 
} 

EDIT

的TEST_TIME表示HH:MM:SS
类型化的数据集是定义为:

public virtual int Update(
        byte EX_ID, 
        byte DOC_ID, 
        byte PATIENT_NO, 
        System.DateTime TEST_DATE, 
        System.TimeSpan TEST_TIME) 
+2

做一个'博士[4] .GetType()',看看是什么返回'包含在'DataRow' – xanatos

+0

类型dt.Columns [4] .DataType'属性? – bradbury9

+0

你试图将哪些数据放入该列中? (即csv中它不喜欢的值是什么) –

回答

2

根据您的输入,dr[4]代表hours:minutes:seconds格式的时间值,我建议您采用以下解决方案。

private TimeSpan GetTimeSpan(string timeString) 
{ 
    var timeValues = timeString.Split(new char[] { ':' }); 
    //Assuming that timeValues array will have 3 elements. 
    var timeSpan = new TimeSpan(Convert.ToInt32(timeValues[0]), Convert.ToInt32(timeValues[1]), Convert.ToInt32(timeValues[2])); 
    return timeSpan; 
} 

使用上述方法如下。

else if (textBox3.Text.Contains("GEN_EX")) 
{ 
    foreach (DataTable dt in result.Tables) 
    { 
     foreach (DataRow dr in dt.Rows) 
     { 
      GEN_EX addtable = new GEN_EX() 
      { 

       EX_ID = Convert.ToByte(dr[0]), 
       DOC_ID = Convert.ToByte(dr[1]), 
       PATIENT_NO = Convert.ToByte(dr[2]), 
       TEST_DATE = Convert.ToDateTime(dr[3]),      
       **TEST_TIME = GetTimeSpan(dr[4].ToString()),** 

      }; 
      conn.GEN_EXs.InsertOnSubmit(addtable); 
     } 
    } 
    conn.SubmitChanges(); 
    MessageBox.Show("File uploaded successfully"); 
} 

这应该给你你想要的价值。如果dr[4]的值不是hours:minutes:seconds格式,您将面临运行时问题。我会把它留给你。

+0

错误'System.Data.DataRow'没有包含'GetString'的定义,也没有找到接受'System.Data.DataRow'类型的第一个参数的扩展方法'GetString'(你是否缺少一个using指令或汇编参考?)\t 我得到这个 – SQLserving

+0

的编译错误哦...我现在意识到了...我已经改变了答案....现在应该不会有任何错误... –

0

首先Timespan和DateTime是2个不同类型,没有隐式转换可用。由于Timespan是两个DateTime之间的时间值,因此您需要知道用于启动Timespan的Mesure的引用时间(DateTime)。

例如,它可能是从DateTime dtReferential = new DateTime(1900, 01, 01);

为了让你需要给它一个C#时间跨度一个SQL时间跨度值!将TEST_TIME值更改为Timespan。最后,给它的参考时间的减价值。

使用前面的例子:

else if (textBox3.Text.Contains("GEN_EX")) 
{ 
    foreach (DataTable dt in result.Tables) 
    { 
     foreach (DataRow dr in dt.Rows) 
     { 
      GEN_EX addtable = new GEN_EX() 
      { 

       EX_ID = Convert.ToByte(dr[0]), 
       DOC_ID = Convert.ToByte(dr[1]), 
       PATIENT_NO = Convert.ToByte(dr[2]), 
       TEST_DATE = Convert.ToTimespan(dr[3]),      
       TEST_TIME = dtReferential.Subtract(Convert.ToDateTime(dr[4])) 

      }; 
      conn.GEN_EXs.InsertOnSubmit(addtable); 
     } 
    } 
    conn.SubmitChanges(); 
    MessageBox.Show("File uploaded successfully"); 
} 
相关问题