locked
Adding a new row to a table (vb.net) RRS feed

  • Question

  •  

    Hi Folks - need to add in a new row to a database table when it is needed.

    anyone know the command i can use?

    cheers!

    Poy.

    Monday, September 3, 2007 2:25 AM

Answers

  • Hi, Poy,

     

    If you want to add a new row to a database table, you should first tell us the DataBase type.

    In .Net applications, Sql DB and Acess DB are often used here.

    1) To add new row to Sql DB.

        Please add System.Data.SqlClient in your project reference.

        Sample code   

    Code Snippet

            Dim conn As New SqlConnection(sql connection string)

            Dim command As New SqlCommand(insert clause, conn)

            Try

                conn.Open()

                If command.ExecuteNonQuery() = 1 Then

                    MessageBox.Show("inserted")

                Else

                    MessageBox.Show("Failed")

                End If

                conn.Close()

            Catch ex As Exception

                conn.Close()

                MessageBox.Show(ex.Message)

            End Try

     

     

    2) To add new row in

       Please add System.Data.OleDB in your project reference

       Sample code

    Code Snippet

            Dim conn As New OleDbConnection(Access connection string)

            Dim command As New OleDbCommand(insert clause, conn)

            Try

                conn.Open()

                If command.ExecuteNonQuery() = 1 Then

                    MessageBox.Show("inserted")

                Else

                    MessageBox.Show("Failed")

                End If

                conn.Close()

            Catch ex As Exception

                conn.Close()

                MessageBox.Show(ex.Message)

            End Try

     

     

     

    More information

    http://msdn2.microsoft.com/en-us/library/system.data.sqlclient(VS.80).aspx

    http://msdn2.microsoft.com/en-us/library/system.data.oledb(VS.80).aspx

     

    Hope this helps,

    Regards

    Wednesday, September 5, 2007 3:53 AM