2017-03-06 11 views
0

Sql Serverには、TEST_TIMEという変数のデータ型がTime(7)としてあります。
C#でテーブルを作成し、自動的にTimespanデータ型を割り当てました。
今、SQLデータベースにCSVファイルのデータをアップロードしようとしていますが、「暗黙のうちに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()'とタイプが 'dt.Columnsを返さないもの 'DataRow' – xanatos

+0

に含まれているかを見る[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'が見つかりませんでした。指令またはアセンブリ参照?)\t コンパイルエラーが発生しました – SQLserving

+0

ああ...今すぐ実現しました...私は答えを変更しました....今はエラーがありません... –

0

最初にTimespanとDateTimeは、暗黙の変換を使用することなく2種類のタイプです。 Timespanは2つのDateTimeの間の時間値なので、Timespanのメッシュを開始するのに使用される参照時間(DateTime)を知る必要があります。

例えば、それはあなたがそれをC#タイムスパンを与える必要がSQLタイムスパン値を与えるためにDateTime dtReferential = new DateTime(1900, 01, 01);

からかもしれません! TEST_TIMEの値をTimespanに変更します。最後に、あなたの参照時間の減算値を与えます。前の例で使用

: `DRを行い

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"); 
} 
関連する問題