Results 1 to 4 of 4

Thread: How to "expand" the INSERT command?

  1. #1

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200

    Question How to "expand" the INSERT command?

    I'm using the INSERT command do add records to my database.

    The problem is that I'm getting an error and I can't know where is it from becouse my database has many fields.

    VB Code:
    1. Dim myCommand As OleDb.OleDbCommand
    2. myCommand.CommandText = "INSERT INTO myTable (myField1,myField2,myField3,myField4,myField5) VALUES ('" & _
    3. TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" TextBox4.Text & "','" & TextBox5.Text & "')"
    4. myCommand.ExecuteNonQuery()

    Is there another way to add records?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Yes , use Row obj as follow :

    VB Code:
    1. Public Overloads Sub SaveDB(ByVal TableStr As String)
    2.         Dim SQLStr = "SELECT * FROM " & TableStr
    3.         Dim MyAdapter As New OleDb.OleDbDataAdapter(SQLStr, MyConnection)
    4.         Dim builder As New OleDbCommandBuilder(MyAdapter)
    5.         Dim mydataset As New DataSet
    6.  
    7.         MyAdapter.Fill(mydataset, TableStr)
    8.         Dim MyDataRow As DataRow = mydataset.Tables(TableStr).NewRow
    9.  
    10.         'Change these colums to adapt yours .
    11.         MyDataRow("Column1") = "Value1"
    12.         MyDataRow("Column2") = "Value2"
    13.         MyDataRow("Column3") = "Value3"
    14.         MyDataRow("Column4") = "Value4"
    15.  
    16.         mydataset.Tables(TableStr).Rows.Add(MyDataRow)
    17.         MyAdapter.Update(mydataset, TableStr)
    18.         MyAdapter = Nothing
    19.         MessageBox.Show("Data Saved..")
    20.     End Sub

  3. #3

    Thread Starter
    Addicted Member AlvaroF1's Avatar
    Join Date
    Sep 2002
    Location
    SP - Brazil
    Posts
    200
    Thanks.

    I was using a code just like yours but I was getting sobre problems when update a record. So I decided to use just INSERT and UPDATE commands.

    Do you have an example to update a record?


    Thank you.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width