Solved

SQL queries using VB 2010

Posted on 2011-03-24
6
665 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

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

23 Experts available now in Live!

Get 1:1 Help Now