Connect to SQL server

G

Guest

Hi all,

I'm trying to connect to an SQL database from my vb.net form, but getting an
error message. The code I used is the following:

Private Sub ConnectSQL_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ConnectSQL.Click
Dim myConnection As SqlClient.SqlConnection
myConnection.ConnectionString = "Persist Security
Info=true;Integrated Security=false;User
ID=sa;Password=xxxxx;database=yyy;server=zzzzz\mmmmm"
myConnection.Open()
End Sub

What am I missing?
 
C

Chris, Master of All Things Insignificant

What error message are you getting? Did you try making the connection in
Server Explorer?

Chris
 
H

Herfried K. Wagner [MVP]

TS said:
I'm trying to connect to an SQL database from my vb.net form, but getting
an
error message. The code I used is the following:

Private Sub ConnectSQL_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ConnectSQL.Click
Dim myConnection As SqlClient.SqlConnection
myConnection.ConnectionString = "Persist Security
Info=true;Integrated Security=false;User
ID=sa;Password=xxxxx;database=yyy;server=zzzzz\mmmmm"
myConnection.Open()
End Sub

For possible connection strings, see
<URL:http://www.connectionstrings.com/>. What error message are you
getting?
 
M

Marc Miller

You need to instantiate your connection as a 'New SQLClient.SQLConnection'.

Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection

You should use a 'Try Catch' to trap your errors. You would have received
the
error:
'Object reference not set to an instance of an object.'
M. Miller
 
C

chanmmn

Example:

Dim strConnectionString As String =
"server=(local);database=pubs;Trusted_Connection=yes"

chanmm
 
C

Cor Ligthert

TS,

Because there are more answers, have a look at the answer from Marc, the
others two can nock at there head when they see what they wrote while it was
so easy. (Not that it is important they both give mostly great answers and I
make those mistakes as well.)

:)

Cor
 
G

Guest

Chris,
Thanks for your reply. I coudn't establish the connection from Server
Explorer because I have vb.net standard edition
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top