DataSet轉RecordSet的Function

摘要:DataSet轉RecordSet的Function

小喵目前正值.NET與ASP的混用期

因此目前撰寫的元件,可能會被以前的ASP程式呼叫到,於是這衍生了一個問題
以前的ASP看不懂DataSet,以前的VB6元件也看不懂DataSet

因此小喵寫了個小小的Function來轉換,相關程式碼如下


        '************************************************************************* 
        '**     撰寫者:                撰寫日期:2007/6/4 
        '**     用途:  1.DataSet 轉 RecordSet 
        '**     做法: 
        '**             1. 
        '**     注意事項: 
        '**             1. 
        '**             2. 
        '**     維護記錄: 
        '**         維護者:姓名(員工代號)     維護日期:日期 
        '**         維護項目: 
        '**                 1. 
        '**                 2. 
        '**         做法:  1. 
        '**                 2. 
        '**         注意事項: 
        '**                 1. 
        '************************************************************************* 
        Try 
            Dim rs As Object = CreateObject("ADODB.Recordset") 
 
            If Ds.Tables(0).Rows.Count > 0 Then 
                Dim x, y As Integer 
                Dim ColName As String = "" 
 
                For x = 0 To Ds.Tables(0).Columns.Count - 1 
                    ColName = Ds.Tables(0).Columns(x).ColumnName 
                    rs.Fields.Append(ColName, 200, 255) 
                Next 
                rs.Open() 
 
                For y = 0 To Ds.Tables(0).Rows.Count - 1 
                    rs.AddNew() 
                    For x = 0 To Ds.Tables(0).Columns.Count - 1 
                        If IsDBNull(Ds.Tables(0).Rows(y).Item(x)) Then 
                            rs.Fields(x).Value = "" 
                        Else 
                            rs.Fields(x).Value = Ds.Tables(0).Rows(y).Item(x) 
                        End If 
                    Next 
                Next 
            End If 
 
            Return rs 
 
        Catch ex As Exception 
            Throw New Exception(ex.Message.ToString) 
 
        Finally 
            'Ds.Dispose() 
 
        End Try 
    End Function 
 

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~2019/6) 
topcat
Blog:http://www.dotblogs.com.tw/topcat