Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I run an sql statement in visual basic 2005

Status
Not open for further replies.

SeaweedOPM

Technical User
Nov 19, 2004
53
US
In access 2003 I was using:
DoCmd.RunSQL "statement here"
or setting the statement to a String SQL and:
DoCmd.RunSQL SQL

The only thing I have found in Visual Basic 2005 is the SqlCommand. I'm just not sure how to use it to run an actual statement.

This is probably an easy fix, please help.
 
First thing is to establish a connection to the database using the SQLConnection object found in the System.Data.SQLClient Namespace. Create a sqlcommand, give the command statement and connection and execute it.

This is for a command that doesn't return a result set

Imports System.Data.SqlClient
Dim SQLConn as new SQLConnection("connectionstring")
SQLConn.open
dim SQLcmd as new SQLCOmmand("commandtext", SQLConn)
SQLcmd.executenonquery

If you wish to return a result set you can fire the command into a datareader

dim dr as sqldatareader = sqlcmd.executereader

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top