How to convert dynamically convert a dataset to a defined dataset
-
Hi, not sure if the subject is really clear but anyway, what I am trying to do is to create a generic data adapter for the different datasets I am using, I don't like all the code .net crestes to add the parameters to the data adapter. In order to do this I read the necessary columns from the tables in the datasets as I or .net defined them on design time. In my procedures I change the columns of my datatables, so in order to creaste the update command parameters I need to get back to the original dataset. As I don't know how to get the original settings out of the current dataset I decided to Dim a new dataset, just for the sstructure, which is the same Type as the current dataset. This works OK if I know at design time what kind of dataset I need to expect, but then durign development you need to go back and update some kind of Select case statement every time you add a new dataset to your project. I have been trying with Ctype and gettype statements but I do not succeed. Guess it is straight forward and I am just missing at least one thing. Any suggestions? Public Sub dsAction(ByVal dsOrig As DataSet) ' Dim Audit1 As clAudit Dim strSQL As String Dim tb As DataTable Dim cl As DataColumn Dim intCount As Integer Dim strSQLUpd As String Dim strSQLUpdWhere As String Dim strSQLIns As String Dim strSQLInsVal As String 'Dim strSQLDel As String Dim Newdataset As New dataset Dim ts As Type 'Audit1 = New clAudit !! THIS line does not work dataset = CType(Newdataset,gettype(dsorig.gettype.fullname)) Try 'Open the connection. 'Me.OleGenCon.Open() For Each tb In Newdataset.Tables Dim OleDaA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter Dim OleDUpdA As OleDb.OleDbCommand = New OleDb.OleDbCommand Dim OleDInsA As OleDb.OleDbCommand = New OleDb.OleDbCommand strSQLIns = "INSERT INTO " & tb.TableName & "(" strSQLUpd = "UPDATE " & tb.TableName & " SET " 'strSQLDel = "DELETE FROM " & tb.TableName & " WHERE (GEN_ID = ?)" For Each cl In tb.Columns OleDaA.TableMappings(0).ColumnMappings.Add(cl.ColumnName, cl.ColumnName) If Not cl.ColumnName = "TABLE_ID" Then If intCount > 0 Then strSQLIns = strSQLIns & ", "