Solved

VB.Net - Most "Compact" Query Retrieval

Posted on 2012-12-27
3
176 Views
Last Modified: 2012-12-28
Good Day Experts!

I need to execute a Select top 1 *...query and get a couple of field values returned by the query.  

What is the most "compact" way with minimum lines of code that I can achieve my desired results?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
3 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38723783
my favorite version :

create a class for connection to db

create a shared method within this class that fills a datatable with the values from the query

  Public Class clsdb
    public Shared Database As String
    public Shared Server As String
    public Shared UserID As String
    public Shared Password As String

    Public Shared Function connectionString() As String
      Dim cs As String
      If _Server Is Nothing Or _UserID Is Nothing Or _Password Is Nothing Or _Database Is Nothing Then
        throw new Exception ("connectionString: incomplete parameters")
      End If
      cs = "SERVER=" + _Server.ToString + ";" ' async=true;"
      cs &= "User ID=" + _UserID.ToString + ";"
      cs &= "Password=" + Trim(_Password.ToString) + ";"
      cs &= "Initial Catalog=" + _Database.ToString + ";"
      cs &= " Connect Timeout=20"
      Return cs
    End Function
    
    Public Shared Function fillDt(ByVal s As String, ByRef dt As DataTable) As Int32
      Dim da As New SqlDataAdapter
      Dim cmd As New SqlCommand
      Dim cnn As New SqlClient.SqlConnection(connectionString())
      Dim nRecord As Int32 = 0
      Try
        cnn.Open()
        cmd.Connection = cnn
        cmd.CommandText = s
        cmd.CommandTimeout = 0
        da.SelectCommand = cmd
        nRecord = da.Fill(dt)
      Catch ex As Exception
        If cnn.State = ConnectionState.Open Then cnn.Close()
        cnn.Dispose()
        Throw New Exception("Error " & ex.Message)
      End Try
      If cnn.State = ConnectionState.Open Then cnn.Close()
      cnn.Dispose()
      Return nRecord
    End Function
  end class 

Open in new window


then just need to initialize the clsdb connection once in the main form

clsdb.Database = "dbname"
clsdb.Server = "192.168.1.xx\sqlservername"
clsdb.userid= "sa"
clsdb.Password = "myserverpass"

then you can populate datatable with :

clsdb.fillDt("SELECT TOP(1) row1, row2)", mydatatable)

Just one line of code
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 38724354
Something like the following:

Dim cmd As New SqlCommand("SELECT TOP 1...", New SqlConnection("<Your ConnectionString>"))
Dim reader As SqlDataReader

cmd.Connection.Open()
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
reader.Read()
firstValue = reader.GetString(0)	'For a first field that is a String
secondValue = reader.GetInt32(1)	'For a second field that is an Integer
reader.Close()

Open in new window

0
 

Author Closing Comment

by:Jimbo99999
ID: 38726758
Excellent...thanks for the help.

Jimbo99999
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question