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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:55:43+00:00 2026-06-12T16:55:43+00:00

I was wondering what is the most basic way to avoid the following. con.ConnectionString

  • 0

I was wondering what is the most basic way to avoid the following.

    con.ConnectionString = connection_String
    con.Open()
    cmd.Connection = con

    'database interaction here

    cmd.Close()

I keep making those lines all over in my project, but I figure there has to be a better way to save on typing this over and over. It makes the code look even more sloppy than it already is!

Ended up with this, works well for me. Thanks for the help 🙂

Public Sub connectionState()
    If con.State = 0 Then
        con.ConnectionString = connection_String
        con.Open()
        cmd.Connection = con
    Else
        con.Close()
    End If
End Sub
  • 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-06-12T16:55:44+00:00Added an answer on June 12, 2026 at 4:55 pm

    This is where a lot of programmers are tempted to create a “database layer” with a variations on method signatures that look like this:

    Public DataSet ExecuteSQL(ByVal sql As String) As DataSet
    

    That allows you to isolate all that boilerplate connection code in one place. An sql command string goes in, and data comes out. Easy.

    Don’t do it!

    This is headed in the right direction, but has one very big flaw: it forces you to use string manipulation to substitute parameter values into your sql queries. That leads to horrible sql injection security vulnerabilities.

    Instead, make sure you include some mechanism in your methods to prompt for the sql parameters separately. This usually comes in the form of an additional argument to the function, and could be as simple as an array of KeyValuePairs. If you’re comfortable with lambdas, my preferred pattern looks like this:

    Public Iterator Function GetData(Of T)(ByVal sql As String, ByVal addParameters As Action(Of SqlParameterCollection), ByVal translate As Func(Of IDatarecord, T)) As IEnumerable(Of T)
        Using cn As New SqlConnection("connection string"), _
              cmd As New SqlCommand(sql, cn)
    
            addParameters(cmd.Parameters)
    
            cn.Open()
            Using rdr As SqlDataReader = cmd.ExecuteReader()
                While rdr.Read()
                    Yield(translate(rdr))
                End While
            End Using
        End Using
    End Function
    

    To call that function, you would do something like this:

    Dim bigCustomers = GetData("SELECT * FROM Customers WHERE SalesTotal > @MinSalesTotal", _
                       Sub(p) p.Add("@MinSalesTotal", SqlDbType.Decimal, 1000000), _
                       MyCustomerClass.FromIDataRecord)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what is the best or most popular database client tool. Similar to Microsoft's
I'm writing an api and was wondering what's the most pythonic way to do
I was just wondering if most games on the iPhone are done in OpenGL
Learning Spring (3.1.0) and Hibernate (4.1.1). Just wondering what most developers do when handling
I am wondering about the most elegant solution of having a grid with each
I was wondering which DVCS is most conducive to experimentation i.e. branching, etc. I
I was wondering how you find it most easy to setup your controllers. Do
I'm just wondering which method is the most effective if I'm literally just wanting
I was wondering, which are the most commonly used algorithms applied to finding patterns
I was wondering how programmers chose to edit XAML. Most of the programmers I

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.