but!... comming soon!

About Techinforoad.com

Welcome to Techinforoad.com - my website where I will publish various articles, tutorials and how-tos on Microsoft Business Solutions Dynamics NAV, SQL Server, virtualization technology and more.
Home Programming ADO.NET Creating connection to MS SQL Server and running a query
Creating connection to MS SQL Server and running a query
User Rating: / 1
PoorBest 

1) Create a new Visual Basic project
2) Create a Windows Form
3) Add TextBox control [Properties: Name=txtOutput, multiline=true, ReadOnly = true]
4) Add a Button control [Properties: Name=btnGo, Text = Go]
5) Double click button control and insert the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

'create connection
Dim conn As New SqlConnection
conn.ConnectionString = "Data Source = .\SQLEXPRESS; Initial Catalog=AdventureWorks;Integrated Security = true"
conn.Open()

'create command
Dim cmd As SqlCommand = conn.CreateCommand

'specify stored procedure to execute
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT TOP (10) ContactID, FirstName, LastName FROM Person.Contact"


'execute command
Dim rdr As SqlDataReader = cmd.ExecuteReader

While rdr.Read()
txtOutput.Text &= rdr(0).ToString & vbTab
txtOutput.Text &= rdr(1).ToString & vbTab & vbTab
txtOutput.Text &= rdr(2).ToString & vbCrLf
End While

rdr.Close()
cmd.Dispose()
conn.Close()

End Sub
End
Class