Link to home
Create AccountLog in
Avatar of sykotex
sykotexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Visual Basic SQL query

Hi
I am new to VB and would like to query an sql database using any 1,2 or 3 values entered into 3 textbox controls, how can I write this query using visual studio 2010
Avatar of Den_HBR
Den_HBR

What code do you already have?
Imports System.Data.SqlClient

Example using a stored procedure:
            Using connSQL As New SqlConnection(My.Settings.SQLConnection)
                mWork_String_1 = "MAPS_GB_Get_One_Parent_Offer"
                Using cmdSQL As New SqlCommand(mWork_String_1, connSQL)
                    cmdSQL.CommandType = CommandType.StoredProcedure
                    cmdSQL.CommandText = mWork_String_1
                    cmdSQL.Parameters.Add("@Parent_Offer", SqlDbType.Char).Value = mParent_Offer
                    connSQL.Open()
                    cmdSQL.ExecuteNonQuery()
                    daMAPS_GB_Associated_Versions = New SqlDataAdapter(cmdSQL)
                    dtMAPS_GB_Associated_Versions = New DataTable
                    daMAPS_GB_Associated_Versions.Fill(dtMAPS_GB_Associated_Versions)
                    Dim drMAPS_GB_Associated_Versions As DataRow
                    For Each drMAPS_GB_Associated_Versions In dtMAPS_GB_Associated_Versions.Rows
                        txtWomens_Offer.Text = Trim(drMAPS_GB_Associated_Versions("Child1_Offer").ToString)
                        txtMens_Offer.Text = Trim(drMAPS_GB_Associated_Versions("Child2_Offer").ToString)
                        txtOther_Offer.Text = Trim(drMAPS_GB_Associated_Versions("Child3_Offer").ToString)
                        cboYear.Text = Trim(drMAPS_GB_Associated_Versions("Offer_Year").ToString)
                        If drMAPS_GB_Associated_Versions("Offer_Season").ToString = "S" Then
                            rbtSpring.Checked = True
                        Else
                            rbtFall.Checked = True
                        End If
                    Next drMAPS_GB_Associated_Versions
                    cmdSQL.Dispose()
                    connSQL.Close()
                    connSQL.Dispose()
                    GC.Collect()
                End Using
            End Using

Example using a command text to do a select:
            Using connSQL As New SqlConnection(My.Settings.SQLConnection)
                mWork_String_1 = "SELECT * FROM Table1 " _
                                              "WHERE Current_Year = '" & txtYear.Text & "'"
                Using cmdSQL As New SqlCommand(mWork_String_1, connSQL)
                    connSQL.Open()
                    cmdSQL.ExecuteNonQuery()
                    connSQL.Close()
                End Using
            End Using
ASKER CERTIFIED SOLUTION
Avatar of EYoung
EYoung
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of sykotex

ASKER

cheers, looks like what i need
One last thing if my connection string in my web.config is clientsConnectionString
What would the line below be?

Using connSQL As New SqlConnection(My.Settings.SQLConnection)
Not sure I understand your question.  Can you restate it another way?