How To Execute SQL to MsSQL

sjchin

Member
Joined
Mar 21, 2003
Messages
22
Hi guy, I am fresh in VB.net. May i know how to execute SQL string in VB.NET to MsSQL ? Normally what i did in VB6 is
using Connection.Execute "insert into table values ..., so how about in VB.NET? The SQL String maybe in Insert,Update or delete. Thanks and wait for prompts reply.
 
Code:
Dim oConnection As SqlConnection = New SqlConnection("[ Connection string ]")
Dim oCommand As SqlCommand = New SqlCommand("DELETE FROM [table] WHERE [table].id = 0", oConnection)

oConnection.Open()
oCommand.ExecuteNonQuery()
oConnection.Close()
Make sure to import System.Data.SqlClient when you use this code.
 
Derek Stone, Thanks, It help for me very much . Clear in mind. Beside this, May i know if we have 3 button of insert, update and delete in a same form, when user click on each 1 will do different transaction, may i know do i need to open SqlConnection when i start load my form and close on exit, or when i do a transaction just open the SqlConnection and close when the transaction is finish ? Which 1 is faster and preferable ? Thanks and wait for prompts reply.
 
Back
Top