Connect to SQL server

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
What error message are you getting? Did you try making the connection in
Server Explorer?

Chris
 
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?
 
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
 
Example:

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

chanmm
 
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
 
Chris,
Thanks for your reply. I coudn't establish the connection from Server
Explorer because I have vb.net standard edition
 
Back
Top