• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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

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
kwh3856
Asked:
kwh3856
2 Solutions
 
ahmadfuwadCommented:
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
 
123654789987Commented:
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
 
kwh3856Author Commented:
Thank each of you for your assistance.  I will probably use both of your suggestions.

Thanks
Kenny
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now