How do you access the return result of a sql query via a program

Posted on 2004-09-22
Last Modified: 2010-04-23
I need to run a query against a table that will produce an average of a specific column.  Can someone show me the basic syntax to accomplish this and then how do you access the return result?  What is the variable name that is returned?


Question by:kwh3856

Accepted Solution

ahmadfuwad earned 250 total points
ID: 12130366
Imports System.Data.SqlClient

'this is the example with the sqlserver

   dim cn As New SqlConnection
   dim cmd As New SqlCommand
   dim reader As SqlDataReader

'make connection with the sql server

   cn.ConnectionString = "Server=eteam04;Database=dbEmployees;user id=sa;password=password"

'write query and get the result

 cmd.Connection = cn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Select ID,Pass From Login"

        reader = cmd.ExecuteReader

        While reader.Read()
            If reader(0) = TextBox1.Text And reader(1) = TextBox2.Text Then
'reader(0) shows the value of ID and reader(1) shows the value of pass in the login table
            End If

        End While


LVL 10

Assisted Solution

123654789987 earned 250 total points
ID: 12130382
If u are sure that u will get only one value from the database then u can use ExecuteScalar

Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As SqlConnection)
    Dim myCommand As New SqlCommand(myScalarQuery, myConnection)
      Dim value as int
    value =  myCommand.ExecuteScalar() //returns the first column of the first row in the result set returned by the query
End Sub 'CreateMySqlCommand

Author Comment

ID: 12133223
Thank each of you for your assistance.  I will probably use both of your suggestions.


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

803 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