Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:16:31+00:00 2026-05-25T06:16:31+00:00

Which one of these two ways would be more recommended to update a database

  • 0

Which one of these two ways would be more recommended to update a database with a given query string:

Option 1:

Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"
Dim command As New SqlClient.SqlCommand(query, sqlConnection)

Dim params As SqlParameter() = {
    New SqlParameter("@Name", txtName.Value),
    New SqlParameter("@Age", txtAge.Value))
}

Call UpdateDatabase(command, params, NumError, DescError)

Public Sub UpdateDatabase(ByVal command As SqlCommand, ByVal parameters() As SqlParameter, ByRef NumError As Double, ByRef DescError As String)
Try
    For Each parameter In parameters
        command.Parameters.Add(parameter)
    Next
    command.ExecuteNonQuery()
    command.Dispose()
    NumError = 0
    DescError = ""
    Catch ex As Exception
        NumError = Err.Number
        DescError = Err.Description
    End Try
End Sub

Option 2:

Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"
Dim command As New SqlClient.SqlCommand(query, sqlConnection)

command.Parameters.AddWithValue("@Name", txtName.Value)
command.Parameters.AddWithValue("@Age", txtAge.Value)

Call UpdateDatabase(command, NumError, DescError)

Public Sub UpdateDatabase(ByVal command As SqlCommand, ByRef NumError As Double, ByRef DescError As String)
    Try
        command.ExecuteNonQuery()
        command.Dispose()
        NumError = 0
        DescError = ""
    Catch ex As Exception
        NumError = Err.Number
        DescError = Err.Description
    End Try
End Sub

Or is there any other better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:16:31+00:00Added an answer on May 25, 2026 at 6:16 am

    It looks like you’re trying to create a reusable UpdateCommand, which is all well and good. In addtion to not opening the connection, I’m not sure that you’re closing the connection (unless the command.Dispose also closes the connection. You’d be better off to move as much of the db connectivity into the Sub as possible.

    Also, I’m not sure where you’re getting Err. from in your exception block, but I’d recommend designing a better way to handle any exceptions – perhaps log it somewhere.

    Finally, make the Sub a Function and return a bool indicating success/failure:

    Public Function UpdateDatabase(ByVal sql As String, ByVal parameters() As SqlParameter) As Boolean
    
        Dim Successful As Boolean = False
    
        Try
            Using conn As SqlConnection = new SqlConnection(sqlConnection)
                Using command As New SqlCommand(sql, conn)
    
                    command.CommandType = CommandType.Text        
    
                    For Each parameter As SqlParameter In parameters
                        command.Parameters.Add(parameter)
                    Next
    
                    conn.Open()
    
                    command.ExecuteNonQuery()
                    Successful = True
                End Using       
            End Using        
        Catch ex As Exception
            Successful = False
            ' Do something with the exception
        End Try
    
    End Function
    

    You could then do this:

    Dim query As String = "INSERT INTO employee VALUES (@Name, @Age)"
    
    Dim params As SqlParameter() = {
        New SqlParameter("@Name", txtName.Value),
        New SqlParameter("@Age", txtAge.Value))
    }
    
    Dim Updated As Boolean = UpdateDatabase(query, params)
    

    This example assumes sqlConnection is a class level variable holding your connection string. You could also read it directly from the config file if desired.

    If you don’t have any parameters for the command, you’ll need to pass in an empty array (or modify the code in the function to check for params = Nothing):

    Dim params As SqlParameter()
    Dim Updated As Boolean = UpdateDatabase(query, params)  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are the differences between these two, and which one should I use? string
I have these two pieces of code, wich one is more readable? foreach decimal
I'm thinking about it, which of the following two query types would give me
I have a database that has two tables, one of which contains a foreign
I cannot find comparison of these parsing technique. Which one is most commonly used?
I have one std::list<> container and these threads: One writer thread which adds elements
There is a directed graph (not necessarily connected) of which one or more nodes
There are several ways to do this, but I'm not sure which one of
These days there are two main hardware environments for parallel programming, one is multi-threading
I know there are several plugins that do asynchronous processing. Which one is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.