Connecting to SQL Server with VB.NET

Jefferson

Member
Joined
Aug 16, 2005
Messages
21
Programming Experience
Beginner
Hello again,

I am trying to connect to SQL Server with VB.NET to make ajustments to the Microsoft CRM. I have VB.NET 2003 standard edition on an XP box.
Each time i try this, VB.NET kicks out the following error...

"Unable to connect to the Database
It is only possible to connect to SQL Server Desktop engine databases and Microsoft Access Databases with this version of Visual Studio"

Many thanks in advance
Jefferson
 
LEt me gues... you are grabbing a server connection object out of the toolbax and dropping it onto the form, right?

Don't do that. Like the error says, that version of the tool will only allow you to connect to an MSDE database or Access.

Recomendation: code it yourself. Using SQLConnection, SQLCommand, SQLAdapter, and a DataSet you can accolplish the exact same thing. In fact it will probably be easier in the long run as you will have direct control over the code and the action it performs.

Tg
 
Hey,

Thats the next step, Thanks.
Will rather code the connection, but you know how it is, Server Explorer looked like a faster way to do things :)

Rgds
Jefferson
 
It's been my experience that the following formula holds true more often than not.

Faster <> Better

Tg
 
Hi,


Use this code.


Purpose :To retrieve data from the database using Data Reader and Data Commands. When this code is executed, the data in a database is displayed in two textbox controls.



Preparation:



Design a form using .NET environment and place two textbox controls on a form.

Design and create a table using SQL Server 2000.



Name the Database as FinAccounting.

Name the Table as AccountsTable.

Name the form as Form1

Name the controls on the form as Textbox1 and Textbox2.



Tasks:



1. Establish the connection with the Database using Connection object.

2. Execute the command.

3. The data will be read by the DataReader object and the contents of the first record is displayed in the textboxes.







VB.NET:
Imports System.Data.SqlClient
 
Public Class Form1
 
Inherits System.Windows.Forms.Form
 
Dim str_sql_user_select As String = “SELECT * FROM AccountsTable”
 
Dim str_connection As String = “Data Source=VSDOTNET;Integrated Security=SSPI;Initial Catalog=FinAccounting”
 
Dim mycon As SqlConnection
 
Dim comUserSelect As SqlCommand
 
Dim myreader As SqlDataReader
 
 
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As system.EventArgs) Handles 
 
MyBase.Load
 
mycon = New SqlConnection(str_connection)
 
‘Instantiate the commands
 
comUserSelect = New SqlCommand(str_sql_user_select, mycon)
 
TextBox1.Text = “ “
 
TextBox2.Text = “ “
 
mycon.Open()
 
myreader = comUserSelect.ExecuteReader
 
If (myreader.Read = True) Then
 
TextBox1.Text = myreader(0)
 
TextBox2.Text = myreader(1)
 
Else
 
MsgBox(“You have reached eof”)
 
End If
 
End Sub
 
End Class


Regards
Bharathi
Books for programmers
http://www.vkinfotek.com
 
Last edited by a moderator:
Back
Top