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

Posted on 2004-09-22
Medium Priority
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
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

Accepted Solution

ahmadfuwad earned 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

801 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