?
Solved

SQL queries using VB 2010

Posted on 2011-03-24
6
Medium Priority
?
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 5

Accepted Solution

by:
bitref earned 2000 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 2000 total points
ID: 35205411
Did you insert your query in the command CommandText property?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

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