[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Execute insert sql statement in vb.net 2005

Posted on 2007-07-31
5
Medium Priority
?
2,807 Views
Last Modified: 2013-11-26
Hi,

I am trying to get to grips with vb.net 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"
        dbOpen()
        Dim da As New SqlClient.SqlDataAdapter(sql, oConn)
        da.Fill(ds, "Customers")
        DataGridView1.DataSource = ds.Tables("Customers")

error_Handler:
        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)
            dbClose()
            End
        End If

        MsgBox("Success")
        dbClose()
    End Sub
End Class

Best Regards
Lee
0
Comment
Question by:ljhodgett
  • 2
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 19599371
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
End Sub

0
 
LVL 53

Expert Comment

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

Expert Comment

by:Sethi
ID: 19599388
Unlike ASO, the connection object in ADO.net 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.
0
 
LVL 18

Expert Comment

by:Sethi
ID: 19599391
Oops..Dhaest types faster :-))
0
 
LVL 34

Expert Comment

by:Sancler
ID: 19599481
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

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22048420.html

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

Roger
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

831 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