Connection question

  • Thread starter Thread starter Woody Splawn
  • Start date Start date
W

Woody Splawn

In the course of using VS.net I have generally setup my connections by
dragging a SQLDataAdatper to the form and going from there. However, I am
at a point where I would like to begin connecting to the database and
creating DataAdapters and DataSets via code.

On a form I go into the ConnectionString property of a SQLConnection object
and copy the values there. I suppose that I can just copy these values to
my connection string that I create in code, but I can not. I have code that
looks like this:

Dim myConnection As New SqlClient.SqlConnection
myConnection = New SqlConnection
Try
'myConnection = "workstation id=P426;packet size=4096;integrated
security=SSPI;data source=P426;persist security info=False;initial
catalog=Corp"
Catch ex As Exception
End Try

With the code above I get a squiggly under the "Workstation id = P426...."
line and it says, "Value of Type String can not be converted to
System.data.sql.connection"

What do I need to do to write a connection string that is palatable to my
code? What is the easiest way of doing this?
 
Hi

Try in this way:

Dim mySqlConnection as SqlConnection

mySqlConnection = new SqlConnection("server=(local)
\NetSDK;Trusted_Connection=yes;database=northwind")

try
mySqlConnection.Open()
Console.WriteLine("Opened Connection to {0}",
mySqlConnection.ConnectionString)

' Close the connection explicitly
mySqlConnection.Close()
Console.WriteLine("Closed Connection. It is
important to close connections explicitly.")
catch
Console.WriteLine("Couldn't Open Connection to
{0}", mySqlConnection.ConnectionString)
end try

PS: Change connection string.

Ravikanth[MVP]
-----Original Message-----
In the course of using VS.net I have generally setup my connections by
dragging a SQLDataAdatper to the form and going from there. However, I am
at a point where I would like to begin connecting to the database and
creating DataAdapters and DataSets via code.

On a form I go into the ConnectionString property of a SQLConnection object
and copy the values there. I suppose that I can just copy these values to
my connection string that I create in code, but I can not. I have code that
looks like this:

Dim myConnection As New SqlClient.SqlConnection
myConnection = New SqlConnection
Try
'myConnection = "workstation id=P426;packet size=4096;integrated
security=SSPI;data source=P426;persist security info=False;initial
catalog=Corp"
Catch ex As Exception
End Try

With the code above I get a squiggly under
the "Workstation id = P426...."
 
Hi Woody,

try something like this.

Dim szConStr as String

szConStr = "user id=sa;password=;initial
catalog=cnugim;data source=kaustav;Connect Timeout=30"

'Here initial catalog is the name of the database, data
source is the name of the machine

Dim conDB as New SqlConnection(szConStr)

Also check out www.connectionstrings.com

HTH.

Kaustav Neogy
-----Original Message-----
In the course of using VS.net I have generally setup my connections by
dragging a SQLDataAdatper to the form and going from there. However, I am
at a point where I would like to begin connecting to the database and
creating DataAdapters and DataSets via code.

On a form I go into the ConnectionString property of a SQLConnection object
and copy the values there. I suppose that I can just copy these values to
my connection string that I create in code, but I can not. I have code that
looks like this:

Dim myConnection As New SqlClient.SqlConnection
myConnection = New SqlConnection
Try
'myConnection = "workstation id=P426;packet size=4096;integrated
security=SSPI;data source=P426;persist security info=False;initial
catalog=Corp"
Catch ex As Exception
End Try

With the code above I get a squiggly under
the "Workstation id = P426...."
 
Back
Top