2009-11-24 70 views
0

下面的代碼:比較數據集的列類型

try 
     { 
      string strReadDataLine; 
      strReadDataLine = sr.ReadLine(); 

      while (strReadDataLine != null) 
      { 
       string[] strReadDataLineSplited = strReadDataLine.Split(';'); 

       DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow(); 
       DataTable item = thisDataSet.Tables["Repartition"]; 
       for (int i = 0; i < strDataLines.Length; i++) 
       { 
        DataColumn thisColomn = 
           thisDataSet.Tables["Repartition"].Columns[i]; 
        // Here i need to know if the colomn is a string 
        if (thisColomn.DataType.ToString() == "System.String") 
        { 
         thisRow[strDataLines[i]] = strReadDataLineSplited[i]; 
        } 
       } 
       thisRow["ID_USAGER"] = 1; 

       thisDataSet.Tables["Repartition"].Rows.Add(thisRow); 

       strReadDataLine = sr.ReadLine(); 
      } 
      //thisDataAdapter.Update(thisDataSet, "Repartition"); 

     } 

我需要的是知道,如果一列是分配一個數據字符串列的字符串。我得到的是一個argumentException,說「輸入字符串格式不正確,無法在MY_FLOAT colomn中存儲< 2.111>,期望類型是double。」

我真正需要的是將列類型與某種類型進行比較以獲取該類型,然後將該列分配給正確的類型。

我希望這很清楚,因爲我的英語不太好。

+0

thisRow [strDataLines [I] 看起來像你不存儲,你以爲你是 – 2009-11-24 21:35:44

+0

你可以發佈你的表結構和一些樣本數據的數據? – 2009-11-24 21:35:56

回答

0

如果我理解正確,我構建了代碼片段的功能副本並修復它以正確處理類型轉換。你真的只錯過了兩件事:

。 #1 - 您按順序將索引編入列中,並使用該信息獲取列類型。然後設置您索引列的信息我的名字。我在下面引入了'columnName'變量來糾正此問題。

。 #2 - 要正確地將字符串輸入轉換爲所需的列類型,只需使用System.Convert.ChangeType(object,Type)方法,如下所示。

static void Main() 
    { 
     DataSet ds = new DataSet(); 
     DataTable dt = ds.Tables.Add("Repartition"); 
     DataColumn col; 

     col = dt.Columns.Add("ID_USAGER", typeof(int)); 
     col = dt.Columns.Add("TestString", typeof(string)); 
     col = dt.Columns.Add("TestInt", typeof(int)); 
     col = dt.Columns.Add("TestDouble", typeof(double)); 

     string testData = "TestString;TestInt;TestDouble"; 
     testData += Environment.NewLine + "Test1;1;1.1"; 
     testData += Environment.NewLine + "Test2;2;2.2"; 

     Test(ds, new StringReader(testData)); 
    } 

    public static void Test(DataSet thisDataSet, StringReader sr) 
    { 
     string[] strDataLines = sr.ReadLine().Split(';'); 
     string strReadDataLine; 
     strReadDataLine = sr.ReadLine(); 

     while (strReadDataLine != null) 
     { 
      string[] strReadDataLineSplited = strReadDataLine.Split(';'); 

      DataRow thisRow = thisDataSet.Tables["Repartition"].NewRow(); 
      DataTable item = thisDataSet.Tables["Repartition"]; 
      for (int i = 0; i < strDataLines.Length; i++) 
      { 
       string columnName = strDataLines[i]; 
       //#1 Don't use this as Columns[i] may not be Columns[columnName] 
       //DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[i]; 
       DataColumn thisColomn = thisDataSet.Tables["Repartition"].Columns[columnName]; 
       //#2 Assing to the results of the string converted to the correct type: 
       thisRow[strDataLines[i]] = System.Convert.ChangeType(strReadDataLineSplited[i], thisColomn.DataType); 
      } 
      thisRow["ID_USAGER"] = 1; 

      thisDataSet.Tables["Repartition"].Rows.Add(thisRow); 

      strReadDataLine = sr.ReadLine(); 
     } 
    }