Solved

VB.Net - Most "Compact" Query Retrieval

Posted on 2012-12-27
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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