Sunday, 4 December 2011

EXECUTE SQL COMMAND in Visual Basic









------------------------------------------------------------------------------------------------------------
Needed Import Statment:

Imports System.Data.SqlClient
------------------------------------------------------------------------------------------------------------
CASE 1: COMMAND which WILL NOT RETUN ANY ROW. Example: UPDATE


Dim connectionString as String
Dim connection as SqlConnection
Dim commandString as String
Dim command as SqlCommand

connectionString = "Data Source=CPHWIN0151;Initial Dialog=ABC Billing Infor;uid=abc_billing;pwd=abc@123"
commandString = "UPDATE ABCTable set costcenter=12345"

connection = New SqlConnection(connectionString)

Try
    connection.open()
    command = New SqlCommand(commandString,connection)
    command.ExecuteNonQuery()
    command.connection.close()
Catch ex As SQLException   

Finnaly
connection.close()

End Try

------------------------------------------------------------------------------------------------------------

CASE 2: COMMAND WHICH WILL RETURN ROW. Example: SELECT


Dim connectionString as String
Dim connection as SqlConnection
Dim commandString as String
Dim command as SqlCommand


connectionString = "Data Source=CPHWIN0151;Initial Dialog=ABC Billing Infor;uid=abc_billing;pwd=abc@123"
commandString = "SELECT objIdentifier,UID FROM V_MA_ORDER WHERE UID='12345'"

connection = New SqlConnection(connectionString)

Try
    connection.open()   
    command = New SqlCommand(commandString,connection)
   
    Dim dr As SqlDataReader = command.ExecuteReader

    Do While dr.Read()
        objId = dr(0).ToString.Trim
        uid = dr(1).ToString.trim
    Loop
   
    dr.close()
    command.connection.close()
Catch ex As SqlException

Finnaly
connection.close()

End Try

------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment