Wednesday, March 28, 2012

Opening SqlConnection - VB.NET

I used to use rdo in VB6 and now I'm trying to figure out how to use the SqlDataReader class in VB.NET. I want to use an ODBC data source to specify the connection info. I used to give the rdoConnection object a connect string that looked something like this:
"DSN=[data source name];UID=[sql server user];PWD=[pwd]"
I don't understand the connect string given in all the examples I've found (nor does it seem to work on my system...)

mySqlConnection =New SqlConnection("server=(local)\VSdotNET;Trusted_Connection=yes;database=northwind")
Does anyone have any ideas? I'm open to explanations as well as solutions :)

Already tried to look at: ?|||jsmiller, are you receiving an error message?
Can you shell out to DOS (EDIT: i.e. acommand prompt, sheesh my age is showing), and run this command:
osql -L
This will tell you the name of the SQL Servers to which you haveaccess. Whatever is returned is what should go into the server=part of the connection string. If you are still having trouble,please paste your connection string and error message and we will tryto help you through it.

Success! The thing is that I don't want to specify a server name but rather a System DSN... So I think based on a little more research (thanks, I don't want to use a sqlConnection but an odbcConnection. Here is my latest attempt:

Dim myOdbcConnectionAs OdbcConnection

Dim myOdbcCommandAs OdbcCommand

Dim myOdbcDataReaderAs OdbcDataReader

myOdbcConnection =New OdbcConnection("DSN=DSN;User Id=sa;Password=PWD")

myodbccommand =New OdbcCommand(SQLString, myOdbcConnection)


myOdbcDataReader = myOdbcCommand.ExecuteReader(CommandBehavior.CloseConnection)
The program breaks on the myOdbcConnection.Open line, with an error
ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection.
So if I add "Trusted_Connection=true" to the connect string everything seems to work out. At this point, my question is what does the trusted connection variable really mean? Am I making a security mistake?
I'm new to this so any comments welcome.

No comments:

Post a Comment