Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I connect to a sql db 1

Status
Not open for further replies.

jeffmoore

Programmer
Aug 29, 2003
301
US
I have been to and the code doesn't work!!!

The Imports line give me an error....

And I am assuming that this is my connectionstring:

"Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"

if so why is the 3rd line below spelled:
"my connectionstring"
and not
myconnectionstring
no space and no quotes ??

Imports System.Data.SqlClient
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="my connectionstring"
oSQLConn.Open()


thanks
jeff
 
You need the ConnectionString property to be set to the actual connection string, either directly or as a variable.
so:

Code:
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString="Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
oSQLConn.Open() 

'or

Dim MyConnectionString As String = "DataSource=Aron1;Initial Catalog=pubs;UserId=sa;Password=asdasd;"
Dim oSQLConn As SqlConnection = New SqlConnection()
oSQLConn.ConnectionString= MyConnectionString
oSQLConn.Open()
 
okay I pasted the first three lines of your cod in and ....

SqlConnection is not defined .....

the same thing happens with all the code samples from microsoft .... what an i missing???

tia
jeff
 
Make sure you still import the System.Data.SqlClient namespace.
 
how do i do that?
I'm a newbie at this so bear with all the dumb questions...
:)
jeff
 
No worries :)

You import namespaces by using

Code:
Imports System.Data.SqlClient

at the top of your class. This saves you from having to fully qualify each of your objects, i.e.:

Code:
Dim oSQLConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
[code]
 
Okay thanks .... my problem was I put the imports stmt below (inside) the class stmt NOT on the first line of code
Thanks ....


I'll be back !!!!
Jeff

 
In this connection string :

oSQLConn.ConnectionString="Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"

what is the data source supposed to point at?
this connection string was copied from an example. and I dont know what "aron1" is

thanks
jeff
 
I assume that's for a DSN that was set up in the example. An easier (and more portable) way would be to use:
"Initial Catalog=pubs;Data Source=INSERT_SERVER_NAME_HERE;User Id=sa;Password=asdasd;"
for a connection string, and make replace INSERT_SERVER_NAME_HERE with the name of the SQL server instance (probably the computer name it's hosted on).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top