2009-11-24 74 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(); 
     } 
    }