开发者

Convert string to guid in DataColumn

How do you convert a DataColumn of GUIDs开发者_StackOverflow set as type string to a DataColumn of GUIDs where the type is Guid? A column in the source data has the wrong type and I cannot change it there.


Once the datatable has been populated with data, you can't change the column type. There's a few approaches you can take:

  1. If you're using a DataAdapter to fill the table, you can use DataAdapter.FillSchema() to get the structure of the table first. Then you can modify the column type, and then fill it with data.

  2. Take the initial table and clone it, change the column type on the new table, and then use DataTable.ImportRow() to import each row from the old table.

  3. Create a new column, copy the values from the old column to the new column, and delete the old column.


You can add to womp's answer the following option:

  1. After you get the table full of the data you can add a new DataColumn to the Columns of the data table this way:

    DataColumn dc = new DataColumn("GUIDColumnName",typeof(Guid),"StringColumnName");
    DataTableObj.Columns.Add(dc);
    

This will not have a performance hit if your data volume is huge.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜