Solved

VB.Net Select Statement To SQL

Posted on 2014-01-30
8
1,361 Views
Last Modified: 2014-01-31
What is the best way to query SQL Server for a value and pass it to the application?  I am querying SQL for the machine name and I would like to take the value returned from SQL and use it in the code to continue processing.
 Private Sub EnableTarget(ByVal strAction As Action)

        Dim strMessage As String = String.Empty
        Dim strCaption As String = String.Empty
        strConn = "Data Source=" & Target & ";Database=" & master & ";Integrated Security=True"

        database = Target.Text


        con = New SqlConnection(strConn)
        con.Open()


        Dim strQuery, strQuery2, strQuery3 As String

        If strAction = Action.EnableMirroringTarget Then

            Try
                strQuery = "USE master SELECT SERVERPROPERTY ('MachineName') "
                strQuery2 = "ALTER DATABASE " & database & " MachineName"
                
                Dim cmdr As SqlCommand
                Dim cmd As SqlCommand

                cmdr = New SqlCommand(strQuery, con)
                Dim rdr As SqlDataReader = cmdr.ExecuteReader

                If rdr.HasRows Then
                    Do While (rdr.Read())

                    Loop
                    '        MachineName.cmdr.Item("MachineName")

                    '    Loop

                    'Else
                    '    Console.Write("No Record Returned")
                End If
                rdr.Close()


                cmd = New SqlCommand(strQuery2, con)
                cmd.ExecuteNonQuery()

                cmd = New SqlCommand(strQuery3, con)
                cmd.ExecuteNonQuery()

            Catch ex As Exception

Open in new window

0
Comment
Question by:LeVette
8 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 39821628
Since you are returning only one value, the DataReader is a little overkill.

ExecuteScalar is a method of the Command object thas has been created expressly for returning only one value, and would thus be a better choice.

cmdr = New SqlCommand(strQuery, con)
MachineName = Cstr(cmdr.ExecuteScalar)
0
 
LVL 1

Expert Comment

by:kevwit
ID: 39821632
You might try the .Net forum for this. Also your execution account would need to have at least serveradmin SQL permissions so that you could alter those properties.
0
 

Author Comment

by:LeVette
ID: 39821663
Ok, I can use the execute ExecuteScalar.  How will that look?  Is there some sample code?  Something I can follow.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40
ID: 39821683
I gave you the sample code.

Simply call ExecuteScalar as in my example. I assume that MachineName is a String. Then reuse that String as you need.
0
 

Author Comment

by:LeVette
ID: 39821785
I am new to vb.net.  Looking for insight into how to incorporate the execute Scalar in the sample code I provided.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 150 total points
ID: 39823885
Here is your modified code


 Private Sub EnableTarget(ByVal strAction As Action)

        Dim strMessage As String = String.Empty
        Dim strCaption As String = String.Empty
        Dim strMachineName As String
        strConn = "Data Source=" & Target & ";Database=" & master & ";Integrated Security=True"

        database = Target.Text


        con = New SqlConnection(strConn)
        con.Open()


        Dim strQuery, strQuery2, strQuery3 As String

        If strAction = Action.EnableMirroringTarget Then

            Try
                strQuery = "USE master SELECT SERVERPROPERTY ('MachineName') "
                strQuery2 = "ALTER DATABASE " & database & " MachineName"
                
                Dim cmdr As SqlCommand
                Dim cmd As SqlCommand

                cmdr = New SqlCommand(strQuery, con)
      
                strMachineName = cmdr.ExecuteScalar()

                cmd = New SqlCommand(strQuery2, con)
                cmd.ExecuteNonQuery()

                cmd = New SqlCommand(strQuery3, con)
                cmd.ExecuteNonQuery()

Open in new window

0
 

Author Comment

by:LeVette
ID: 39824642
Thank you tons, I corrected my errors and that worked.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

20 Experts available now in Live!

Get 1:1 Help Now