Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've not been on for a while (Off work) so happy new year to you all.

 

I've started looking at ADO.NET and trying inserting a record into a ACCESS2000 database. The problems.... I what to use datasets as In the software i'm creating Relationship Links are a nice idea. The following CODE doesn't work with the error;

 

Syntax error in INSERT INTO statement. Anyone know what im doing wrong???? Also when looking at the SQL in the OleDbCommandBuilder all there is, is ?????????? where the values should be

 

:(

 

       Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\support.mdb"
       Dim SelectString As String = "SELECT * FROM SUPPORT"
       Dim MyConn As New OleDbConnection(ConnectionString)
       Dim MyAdapter As New OleDbDataAdapter()
       MyAdapter.SelectCommand = New OleDbCommand(SelectString, MyConn)
       Dim CommandBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(MyAdapter)

       Dim MyDataset As New DataSet()
       Dim MyTable As New DataTable()
       Try
           MyConn.Open()
           MyAdapter.FillSchema(MyDataset, SchemaType.Source, "Support")
           MyAdapter.Fill(MyDataset, "Support")

           MyTable = MyDataset.Tables("Support")

           Dim MyRow As DataRow = MyTable.NewRow
           MyRow(2) = "Mr D"
           MyTable.Rows.Add(MyRow)

           MsgBox(CommandBuilder.GetInsertCommand.CommandText.ToString)
           MyAdapter.Update(MyDataset, "Support")

       Catch ex As OleDbException
           Console.WriteLine(ex.Message)
       End Try

 

 

Cheers Andy.

Code today gone tomorrow!
  • *Experts*
Posted

The code you have should be working. What are the columns in your table? If any are reserved words, the default "INSERT" statement will fail. For example, if you have a column named "First" instead of "FirstName" it will fail as Access uses First as a reserved word. You CAN do it, but you'll have to build your InsertCommand by hand, wrapping each column name with square brackets. For example:

INSERT INTO SUPPORT ([Col1], [Col2], [First]) VALUES ( ?, ?, ?)

 

Also, you don't need the line:

MyAdapter.FillSchema(MyDataset, SchemaType.Source, "Support")

When you perform .Fill(...), it will create the table for you. If you wanted only the table definition built, you could use FillSchema.

 

You've probably noticed the performance hit when you run the MsgBox line. Once you get your tables setup correctly, it's best to copy the auto-generated SQL statements from the CommandBuilder and insert them manually into your code. That way your code doesn't have to build them every time.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...