Solved

SQL queries using VB 2010

Posted on 2011-03-24
6
670 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

696 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