Code for saving NEW datatable into a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • enforcer6767
    New Member
    • Mar 2008
    • 2

    Code for saving NEW datatable into a database

    Hi all, I am able to save, update, delete etc to data in already existing tables, but i can't seem to work out how to create and save a new table to a database. Using Visual Studio 2005, VB.

    I've tried this and few other ways of coding it, but i can't seem to get it right:
    _______________ _______________ _______________ _________

    Public Class Form1

    Dim con As New OleDb.OleDbConn ection
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim da As New OleDb.OleDbData Adapter
    Dim dr As New DataRowAction
    Dim dc As New DataColumn
    Dim sql As String

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

    dt.TableName = "NewTableTe st1"
    dt.Columns.Add( dc)
    dt.Rows.Add(dr)
    ds.Tables.Add(d t)

    da.Update(ds)

    End Sub

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

    con.ConnectionS tring = "PROVIDER=Micro soft.Jet.OLEDB. 4.0;Data Source = C:\Users\Andrew \AFL2008Schedul eNEW.mdb"
    con.Open()

    sql = "SELECT * FROM AndrewsPicks"
    da = New OleDb.OleDbData Adapter(sql, con)
    da.Fill(ds, "AFL2008Schedul eNEW")

    End Sub
    End Class
    _______________ _______________ _______________ _______

    Error for this code = InvalidOperatio nException, refering to the code "da.Update( ds)"

    Can't seem to find examples of what im looking for anywhere, any help would be appreciated.
    cheers,
    -Andrew
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    You have to execute SQL on your database to create a new table in code like that. There is specific SQL that will do this. (CREATE syntax)

    Sorry! We can't seem to find the resource you're looking for

    Comment

    • enforcer6767
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by jeffstl
      You have to execute SQL on your database to create a new table in code like that. There is specific SQL that will do this. (CREATE syntax)

      http://www.w3schools.com/sql/sql_create.asp
      I'm not familar with SQL stuff, but hopefully i'll beable to pick it up from that website, thanks alot mate, appreciate the help.

      Comment

      Working...