比较数据集列类型

时间:2009-11-24 21:23:03

标签: c# dataset

以下是代码:

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中存储&lt; 2.111&gt;。期望类型是双倍。”

我真正需要的是将列类型与要获取类型的内容进行比较,然后将列分配给正确的类型。

我希望这很清楚,因为我的英语不太好。

1 个答案:

答案 0 :(得分: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();
        }
    }
相关问题