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 644759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:25:41+00:00 2026-05-13T21:25:41+00:00

I ran into something odd, and I’m not precisely sure why it is behaving

  • 0

I ran into something odd, and I’m not precisely sure why it is behaving this way. In a for each loop I am adding rows to a table for a cross reference. Using the following code:

For Each cp In pCheckPoints
    If cp <> String.Empty Then
        Dim insertSQL As New StringBuilder
        With insertSQL
            .Append("INSERT INTO [CheckpointMessage] ( ")
            .Append(" [MessageID] ")
            .Append(", [CheckPoint] ")
            .Append(" ) VALUES ( ")
            .Append(" @MessageID ")
            .Append(", @Checkpoint ")
            .Append(" ) ")
        End With
        Using objCommand As New SqlCommand(insertSQL.ToString, MySQLConnection)
            With objCommand.Parameters
                .AddWithValue("@MessageID", pMessageID)
                .AddWithValue("@Checkpoint", cp)
            End With
            objCommand.ExecuteNonQuery()
            objCommand.CommandText = String.Empty
        End Using
    End If
Next

Without the objCommand.CommandText = String.Empty line the CommandText is appending the insertSQL but that doesn’t make any sense to me because I would expect the objCommand’s commandText to be empty since it is in a using block.

  • 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-13T21:25:41+00:00Added an answer on May 13, 2026 at 9:25 pm

    Your command text is the same every time. Don’t rebuild it. Try this:

    Dim insertSql As String = _
        "INSERT INTO [CheckpointMessage] " & _
            "([MessageID], [CheckPoint]) " & _
            "VALUES " & _
            "(@MessageID, @ChceckPoint)"
    
    Using cmd As New SqlCommand(insertSql, MySQLConnection)
        cmd.Parameters.Add("@MessageID", SqlDbType.Int).Value = pMessageID
        cmd.Parameters.Add("@CheckPoint", SqlDbType.NVarChar, 255) ''# I had to guess at this type
    
        For Each cp As String In pCheckPoints.Where(Function(c) Not String.IsNullOrEmpty(c))
            cmd.Parameters("@CheckPoint").Value = cp
            cmd.ExecuteNonQuery()
        Next cp
    End Using
    

    It’s better for a lot of reasons:

    • The compiler can optimize your string concatenations away, where the StringBuilder forced it do that work at run time
    • Explicitly typed parameters avoid a few edge cases that can really kill performance in sql server, or even break your query.
    • This only creates your insert query string once, not once per checkpoint
    • This only creates one SqlCommand object
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.