Question : Copy Tables from Runtime Dataset to Empty Dataset (MDB)

Please help..  Here is what I am trying to accomplish.  I have an application that fills a datagrid based on event calls from the users ( i.e user presses button, application fills empty datagrid with data from specific sql query).  My next task is to take the data in that datagrid and copy it out to a MDB file for portability (user can take that MDB file back to office and extract data at will).  The final step will be to take a set of SQL tables and copy them all out at once to the MDB.  I have an empty MDB in my project and an empty dataset created for it.

When my messagebox pops below I can see the table count has increased, but when I open the MDB the new table is not there??
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
'Early on I have code that fills the datagrid from a shared SQL Class.
'the datatable that is created is a new datatable named Me.dt
'The empty MDB dataset is me.StoreExportDataSet
 
Private Sub btnFillMDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFillMDB.Click
 
        For Each tbl As DataTable In Me.dt.DataSet.Tables()
            Me.StoreExportDataSet.Tables.Add(tbl.Copy)
        Next
 
        Me.StoreExportDataSet.AcceptChanges()
        MsgBox("TABLE COUNT: " & Me.XStoreExportDataSet.Tables.Count.ToString)
       
  End Sub

Answer : Copy Tables from Runtime Dataset to Empty Dataset (MDB)

The Field PassWord fails in OleDb. In MsAccess do not give me error.
I Enclosed it by "[", "]"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
  Public Shared Function ExportDsToMsAccess(ByVal Name As String, ByVal DT As DataTable, ByVal db As OleDb.OleDbConnection) As Boolean
        Dim Sb As New StringBuilder(500)
        Sb.Append("Create Table [").Append(Name).Append("] (")
        For Each c As DataColumn In DT.Columns
           Dim Tc = Type.GetTypeCode(c.DataType)
           Sb.Append("[").Append(c.ColumnName).Append("]").Append(" "c)
           Select Case Tc
             Case TypeCode.String
                  If c.MaxLength > 255 OrElse c.MaxLength < 0 _
                     Then Sb.Append("LONGTEXT") _
                     Else Sb.AppendFormat("Text({0})", c.MaxLength)
             Case TypeCode.DateTime : Sb.Append("DateTime")
             Case TypeCode.Int16 : Sb.Append("SHORT")
             Case TypeCode.Int32 : Sb.Append("INT")
             Case TypeCode.Single : Sb.Append("REAL")
             Case TypeCode.Double : Sb.Append("FLOAT")
             Case TypeCode.Decimal : Sb.Append("NUMBER")
             Case TypeCode.Char : Sb.Append("Text(1)")
             Case TypeCode.Boolean : Sb.Append("BIT")
             Case TypeCode.Byte : Sb.Append("BYTE")
             Case Else
                 If c.DataType Is GetType(Guid) Then
                     Sb.Append("Text(50)")
                 Else
                     Throw New Exception(Tc.ToString & " Tipo no Implementado, Columna: " & c.ColumnName)
                 End If
           End Select
           Sb.Append(","c)
        Next
        Sb.Chars(Sb.Length - 1) = ")"c
        Try
            Dim cmd As New OleDb.OleDbCommand("Drop Table " & Name, db)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
        End Try
        Try
           Dim cmd As New OleDb.OleDbCommand(Sb.ToString, db)
           cmd.ExecuteNonQuery()
 
   
           Dim da As New OleDb.OleDbDataAdapter("Select * from " & Name, db)
           Dim cb As New OleDb.OleDbCommandBuilder(da)
           cmd = cb.GetInsertCommand
           Sb.Length = 0
           Sb.Append("Insert into [").Append(Name).Append("] Values(")
           For n = 1 To DT.Columns.Count : Sb.Append("?,") : Next
           Sb.Chars(Sb.Length - 1) = ")"c
           cmd.CommandText = Sb.ToString
 
           Dim t As New Stopwatch
           t.Start()
           For Each r As DataRow In DT.Rows
             For n = 0 To DT.Columns.Count - 1
                 Dim v = r(n)
                 If TypeOf (v) Is Guid Then v = v.ToString
                 cmd.Parameters(n).Value = v
             Next
             cmd.ExecuteNonQuery()
           Next
 
           t.Stop()
           Debug.Print(t.Elapsed.ToString)
 
        Catch ex As Exception
           MessageBox.Show(ex.Message, "Creating Ms Acces Table " & Name)
           Debug.Print(Sb.ToString)
           Return False
        End Try
        Return True
  End Function
Random Solutions  
 
programming4us programming4us