Execute insert sql statement in 2005

Posted on 2007-07-31
Last Modified: 2013-11-26

I am trying to get to grips with and sql server.  I have managed to retrieve data into a dataset like I would in vb6 but using record sets but I am not too sure how to execute an insert statement as obviously it doesn't need a dataset. The code I have used for the dataset is: -

Public Class frmAddProduct
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error GoTo error_Handler

        Dim ds As New DataSet
        Dim sql As String
        sql = "Select * from Customers"
        Dim da As New SqlClient.SqlDataAdapter(sql, oConn)
        da.Fill(ds, "Customers")
        DataGridView1.DataSource = ds.Tables("Customers")

        If Err.Number <> 0 Then
            MsgBox("An error has occured while connecting to the SQL Server. Error no: " & Err.Number & vbCrLf & vbCrLf & "Description:" & vbCrLf & Err.Description)
        End If

    End Sub
End Class

Best Regards
Question by:ljhodgett
    LVL 53

    Accepted Solution

    Public Sub CreateCommand(ByVal queryString As String, _
      ByVal connectionString As String)
        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, connection)
        End Using
    End Sub

    LVL 53

    Expert Comment

    CreateCommand ("insert into <table> values (1,2,3) ", "Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;")
    LVL 18

    Expert Comment

    Unlike ASO, the connection object in doesn't have the execute method to execute a query. However th command object has a similar method called ExecuteNonQuery. You can use this method to execute your insert statement.
    LVL 18

    Expert Comment

    Oops..Dhaest types faster :-))
    LVL 34

    Expert Comment

    How are you creating the new record that you want to insert?  Because, if it created in the datatable, or created in a bound control and so gets into the datatable, you would be able to use the dataadapter to do the insert.  Have a look at the last post in this thread

    Although it's coded for OleDb it is just applicable, with the terminology changed, to SQL


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now