User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 373,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,807 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser:
Apr 29th, 2007
Views: 9,406
Connecting and using SQL in VB.NET
Last edited : Apr 29th, 2007.
vbnet Syntax | 5 stars
  1. 'Declare outside of class
  2. Imports System.Data.SqlClient
  3.  
  4.  
  5. 'Declare inside of class >
  6. Dim SQLStr As String
  7. Private ConnString As String
  8.  
  9. 'Connstring = Server Name, Database Name, Windows Authentication
  10. connstring = "Data Source=myserver;Initial Catalog=databasename;Integrated Security=True"
  11.  
  12. 'SQL Staments
  13.  
  14. 'SQL query = myQuery = "SQL Statment"
  15.  
  16. SQLStr = "SELECT * FROM tblQuestion"
  17.  
  18. SQLStr = "INSERT into tblQuestion(Name, Question) VALUES('Fred', 'How to use SQL?')"
  19.  
  20. SQLStr = "UPDATE tblQuestion SET Answer = 'Like this' Where Question = 'How to use SQL?'"
  21.  
  22. SQLStr = "DELETE FROM tblQuestion WHERE Question='How to use SQL?'"
  23.  
  24. 'Write to SQL
  25.  
  26. Dim SQLConn As New SqlConnection() 'The SQL Connection
  27. Dim SQLCmd As New SqlCommand() 'The SQL Command
  28.  
  29. SQLConn.ConnectionString = ConnString 'Set the Connection String
  30. SQLConn.Open 'Open the connection
  31.  
  32. SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
  33. SQLCmd.CommandText = SQLStr 'Sets the SQL String
  34. SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only
  35.  
  36. SQLConn.Close() 'Close the connection
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. 'Read from SQL
  47.  
  48. Dim SQLConn As New SqlConnection() 'The SQL Connection
  49. Dim SQLCmd As New SqlCommand() 'The SQL Command
  50. Dim SQLdr As SqlDataReader 'The Local Data Store
  51.  
  52. SQLConn.ConnectionString = ConnString 'Set the Connection String
  53. SQLConn.Open 'Open the connection
  54.  
  55. SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
  56. SQLCmd.CommandText = SQLStr 'Sets the SQL String
  57. SQLdr = SQLCmd.ExecuteReader 'Gets Data
  58.  
  59. While dr.Read() 'While Data is Present
  60. MsgBox(dr("Column Name")) 'Show data in a Message Box
  61. End While
  62.  
  63. Loop While SQLdr.NextResult() 'Move to the Next Record
  64. SQLdr.Close 'Close the SQLDataReader
  65.  
  66. SQLConn.Close() 'Close the connection
Comments (Newest First)
majestic0110 | Veteran Poster | Apr 15th, 2008
Nice snippet, the comments are especially useful to gain a better understanding of what is going on. Good work!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 7:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC