By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,917 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,917 IT Pros & Developers. It's quick & easy.

Want connect VB.NET console application to SQL server

P: 1

I am new to VB.Net. I want to connect my VB.Net project to SQL server to pass the data. How do I do that?

Appreciate your help with an example.
May 8 '14 #1
Share this Question
Share on Google+
1 Reply

P: 299
The same way you do with a Windows App :)

Expand|Select|Wrap|Line Numbers
  1. Module Module1
  3.     'specify connection details.  If your domain accounts can't connect to the server/db you will need to provide credentials
  4.     Dim sqCon As New SqlClient.SqlConnection("Server=SERVERNAME;;Database=DATABASENAME; Trusted_Connection=Yes;")
  5.     Dim sqCmd As New SqlClient.SqlCommand
  6.     Dim sdrRow As SqlClient.SqlDataReader
  8.     Sub Main()
  9.         'the next 5 rows are pretty self explanatory
  10.         sqCmd.Connection = sqCon
  11.         sqCmd.CommandText = "select top 10 * from TABLE"
  12.         sqCon.Open()
  13.         sqCmd.ExecuteNonQuery()
  14.         sdrRow = sqCmd.ExecuteReader()
  15.         For Each itm In sdrRow   'check each returned result from your query
  16.             Dim column0 As String = sdrRow.GetValue(0)   'specify which column value you want
  17.             MsgBox(column0)
  18.         Next
  19.         'make sure you close your connections!
  20.         sdrRow.Close()
  21.         sqCon.Close()
  22.     End Sub
  24. End Module
May 8 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.