Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-22
3
Medium Priority
?
268 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
[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 2

Accepted Solution

by:
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"
            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 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)
    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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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