cast untype dataset to type dataset

这几天项目遇到个问题,untype dataset需要转换为 type dataset ,用(type dataset )untype dataset报错,百度一下也没找到,不得已自己用了最笨的数据复制

_ds = new MailSearchResultDataSet();
         

            
for (int i = 0; i < dsd.Tables[0].Rows.Count; i++)
            
{
                MailSearchResultDataSet.V_ALL_MAILSRow dr 
= _ds.V_ALL_MAILS.NewV_ALL_MAILSRow();
                
for (int j = 0; j < _ds.V_ALL_MAILS.Columns.Count; j++)
                
{
                    
if (dsd.Tables[0].Rows[i][_ds.V_ALL_MAILS.Columns[j].ColumnName].ToString() != "")
                    
{
                        dr[_ds.V_ALL_MAILS.Columns[j].ColumnName] 
= dsd.Tables[0].Rows[i][_ds.V_ALL_MAILS.Columns[j].ColumnName];
                    }


                }


                _ds.V_ALL_MAILS.Rows.Add(dr);
            }


后来不死心google 了一下,发现可以如下(引用原文)

方法一:
Re: Unable to cast object of type 'System.Data.DataSet' to Typed DataSet 
--------------------------------------------------------------------------------

The generic dataset returned cannot be 
"Cast" because it doesn't inherit or
derived from your Typed Dataset.

Look into the 
"Merge" method of the typed dataset to read-in data from the
generic untyped dataset. If the tables and columns match it should pour the
data into the appropriate corresponding places 
in your typed dataset.

方法二:
This is a problem with Enterprise Library. The only solution I have found is as follows 

1) loop through the untyped datasets tables and and assign them the name from the type dataset tables

2) Set the untyped dataset namespace to the same as the typed dataset namespace.

3) write the untyped dataset to xml in a memory stream IE: ds.writexml(ms)

4) with a typed dataset read the xml memory stream IE: typedds.readxml(ms)


方法三:
// load the generic data from the data layer into a strongly typed dataset
dsTyped.Load(dsUntyped.Tables[0].CreateDataReader(), 
LoadOption.OverwriteChanges, dsTyped.mytablename);

还是 google好

posted on 2008-01-10 11:32  Above The Sky  阅读(265)  评论(0编辑  收藏  举报

导航