Solved

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

Posted on 2004-09-22
3
249 Views
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?

Thanks
Kenny

0
Comment
Question by:kwh3856
3 Comments
 
LVL 2

Accepted Solution

by:
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"
            cn.Open()

'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

        reader.Close()

0
 
LVL 10

Assisted Solution

by:123654789987
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)
    myCommand.Connection.Open()
      Dim value as int
    value =  myCommand.ExecuteScalar() //returns the first column of the first row in the result set returned by the query
   myConnection.Close()
End Sub 'CreateMySqlCommand
0
 

Author Comment

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

Thanks
Kenny
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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 video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now