Skip to content

SQL Insert With Parameters Using a SQL Query or Stored Procedure

January 2, 2012
'###########################################
'      SQL Insert VB Code
'###########################################
'With SQL Query
Public Shared Function InsertData(ByVal Parameter00 As String, ByVal Parameter01 As String) As Boolean
    InsertData = True
    Try
        Using cmd As New SqlCommand("INSERT INTO tblData ( Parameter00, Parameter01 ) VALUES ( @Parameter00, @Parameter01 )", New SqlConnection(Conn))
            cmd.Parameters.AddWithValue("@Parameter00", Parameter00)
            cmd.Parameters.AddWithValue("@Parameter01", Parameter01)
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Dispose()
        End Using
        InsertData = True
    Catch ex As Exception
        NotifyAdmin(String.Format("Computer: {0}</br>Application: {1}</br>Function/Sub: {2}</br>Error: {3}", Environment.MachineName, Application.ProductName, GetCurrentMethod.Name, ex))
    End Try
End Function

'With Stored Procedure
Public Shared Function InsertData(ByVal Parameter00 As String, ByVal Parameter01 As String) As Boolean
    InsertData = True
    Try
        Using cmd As New SqlCommand("spInsertData", New SqlConnection(Conn)) With {.CommandType = CommandType.StoredProcedure}
            cmd.Parameters.AddWithValue("@Parameter00", Parameter00)
            cmd.Parameters.AddWithValue("@Parameter01", Parameter01)
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Dispose()
        End Using
        InsertData = True
    Catch ex As Exception
        NotifyAdmin(String.Format("Computer: {0}</br>Application: {1}</br>Function/Sub: {2}</br>Error: {3}", Environment.MachineName, Application.ProductName, GetCurrentMethod.Name, ex))
    End Try
End Function
    
--###########################################
--      SQL Insert SQL Code
--###########################################
CREATE PROCEDURE spInsertData
    @Parameter00 VARCHAR(50) ,
    @Parameter01 VARCHAR(50)
AS 
    BEGIN          
        INSERT  INTO tblData ( Parameter00, Parameter01 )
        VALUES  ( @Parameter00, @Parameter01 )   	  
    END            
     
Advertisements

From → .Net, SQL

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: