?
Solved

VB.Net - Most "Compact" Query Retrieval

Posted on 2012-12-27
3
Medium Priority
?
184 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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