Home    
Log In    
Share Code    
Join    

Navigation
 Skip Navigation Links.

Forums
Skip Navigation Links.

Search

 

Advertisement
 
VB.NET Source Code > How to use SQL Source Code
Details
Author: ptaylor
Views: 3414

How to use SQL
Description
How to use SQL statments in Visual Basic.NET

Read (SELECT)
Write (INSERT, UPDATE, DELETE)
 
 
Code:
'Declare outside of class
Imports System.Data.SqlClient

'Declare inside of class >
Dim SQLStr As String
Private ConnString As String

'Connstring = Server Name, Database Name, Windows Authentication
connstring = "Data Source=myserver;Initial Catalog=databasename;Integrated Security=True"

'SQL Staments

'SQL query = myQuery = "SQL Statment"

SQLStr = "SELECT * FROM tblQuestion"

SQLStr = "INSERT into tblQuestion(Name, Question) VALUES('Fred', 'How to use SQL?')"

SQLStr = "UPDATE tblQuestion SET Answer = 'Like this' Where Question = 'How to use SQL?'"

SQLStr = "DELETE FROM tblQuestion WHERE Question ='How to use SQL?'"

  

'Write to SQL

Dim SQLConn As New SqlConnection() 'The SQL Connection
Dim SQLCmd As New SqlCommand() 'The SQL Command

SQLConn.ConnectionString = ConnString 'Set the Connection String
SQLConn.Open 'Open the connection

SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = SQLStr 'Sets the SQL String
SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only

SQLConn.Close() 'Close the connection

  

'Read from SQL

Dim SQLConn As New SqlConnection() 'The SQL Connection
Dim SQLCmd As New SqlCommand() 'The SQL Command
Dim SQLdr As SqlDataReader 'The Local Data Store

SQLConn.ConnectionString = ConnString 'Set the Connection String
SQLConn.Open 'Open the connection

SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = SQLStr 'Sets the SQL String
SQLdr = SQLCmd.ExecuteReader 'Gets Data

Do
While dr.Read() 'While Data is Present

    MsgBox(dr("Column Name")) 'Show data in a Message Box  

End While
Loop While SQLdr.NextResult() 'Move to the Next Record

SQLdr.Close 'Close the SQLDataReader
SQLConn.Close() 'Close the connection  
Date Added: 02/09/2007 13:04:00





Comments


If you log in you can add a comment of your own!
 
 
Welcome Guest
 
  Log In Name
 
  Password
 
 
Statistics
Your Details: Not Logged In
  Join Date: N/A
  Last Logon: N/A
  Code Snippets: N/A
  Code Comments: N/A
  Forum Posts: N/A
  Forum Subjects: N/A

Site Wide
  Code Snippets: 32
  Code Comments: 2
  Forum Posts: 52
  Forum Subjects: 24
 
Random Code
How to Multi-Thread
Multi-Threading allows you to have multiple runs o....

Language: VB.NET

 
Advertisement


All the Source Code and Tutorials on this site are free for you to use as you require.