Solved

SQL queries using VB 2010

Posted on 2011-03-24
6
668 Views
Last Modified: 2013-12-25
Although OK with VB, I have never coded anything to connect to SQL and run queries.

I have my connection already as "conn"

What I want to do is run a query and store the result in a variable.

My query is:-

SELECT SUM(timetaken) FROM dbo.ACTIONS where who='roybridge' and Whe_ >= '03/01/2011'

Open in new window


How do I do this in VB 2010?

Thanks
0
Comment
Question by:Chris Millard
  • 3
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
bitref earned 500 total points
ID: 35205024
        'Declare Connection
        Dim conn As New SqlConnection

        'Set connection string
        conn.ConnectionString = ""
        'Declare command and set its connection, type and SQL test (Query)
        Dim cmd As New SqlCommand
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = ""

        'Declare output string
        Dim strOutput As String

        'Open the connection, fill the output string and close the connection
        conn.Open()
        strOutput = cmd.ExecuteScalar
        conn.Close()

Open in new window

0
 
LVL 17

Author Comment

by:Chris Millard
ID: 35205353
When the code gets to

strOutput = cmd.ExecuteScalar

I get an error "ExecuteScalar: CommandText property has not been initialized"

0
 
LVL 5

Assisted Solution

by:bitref
bitref earned 500 total points
ID: 35205411
Did you insert your query in the command CommandText property?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Author Comment

by:Chris Millard
ID: 35205491
This is the full code:-

        Dim conn As New SqlConnection()
        conn.ConnectionString = "Data Source=RCSRV01;Initial Catalog=NetHDRedcatSQL;Persist Security Info=True;User ID=sa;Password=letmein"

        Dim cmd As New SqlCommand
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = ""

        Dim strOutput As String

        conn.Open()
        strOutput = cmd.ExecuteScalar
        conn.Close()

Open in new window

0
 
LVL 5

Assisted Solution

by:bitref
bitref earned 500 total points
ID: 35205557
Put your query in the command CommandText:
cmd.CommandText = "SELECT SUM(timetaken) FROM dbo.ACTIONS where who='roybridge' and Whe_ >= '03/01/2011'"

Open in new window

0
 
LVL 17

Author Comment

by:Chris Millard
ID: 35205648
D'oh! Stupidly I didn't see that!!!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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