0

I have a dataset populated from a database:

dataset_original = new DataSet()
data_adapter.Fill(dataset_original)

and I cloned it:

dataset_cloned = dataset_original.Clone()

I cloned it because 1 of the columns in the original is of type int, and I want to change that to type string:

dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)

Now I need to populate the new dataset with the data from the old dataset. How do I do that?

I am using asp.net 1.1 coded with vb.net.

4

2 回答 2

1

这个简单的循环应该可以工作(即使使用OPTION STRICT ON):

Dim dataset_cloned = dataset_original.Clone()
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
For i As Int32 = 0 To dataset_original.Tables.Count - 1
    Dim tbl_original As DataTable = dataset_original.Tables(i)
    Dim tbl_cloned As DataTable = dataset_cloned.Tables(i)
    For Each row As DataRow In tbl_original.Rows
        tbl_cloned.Rows.Add(row.ItemArray)
    Next
Next
于 2013-09-11T10:10:31.917 回答
0

假设您在数据集中只有一个表,您可以这样做。

            int ColumnIndex = 0; //Column index of your data you want to copy
            for (int i = 0; i < dataset_original.Tables[0].Rows.Count; i++)
            {
                dataset_cloned.Tables[0].Rows[i].SetField(ColumnIndex, dataset_original.Tables[0].Rows[ColumnIndex].ItemArray[0].ToString());
            }

在同一个 for 循环中,您可以复制剩余的列数据

于 2013-09-11T10:03:26.523 回答