Question Locked

Connection string reset - VB.NET

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm trying to create a utility in VB.net to connect to a SQL Server database, run several queries, and display the results in text boxes.

The main form has login text boxes for Server name, database, user, and password. It also has a button to disconnect from the database after the information has been gathered.

I want to be able to reset the connection information so I can re-enter the login using different parameters and connect to a different server or database.

The app seems to store the connection information so it continues to connect to the first database even though I have changed the login settings and logged back in.

Is there a way to reset the connection information without having to close the app and restart it?

5 replies

by tony hopkinson

It's better to cfeate a connection with the current parameters each time you need one. Do the query, save the result and then let the lot go out of scope.

So just instantiateb a new one each time, let the GC bin the old one now it's not referenced.

by puppybreath

can you be more specific on what "let the GC bin the old one now it's not referenced" means?

I'm thinking there may be a typo in there somewhere. :-)

Thanks.

by tony hopkinson

We do something like this in C#

Static Class MyDB
{
static SQLDataReader CreateReader(String ConnectionString, String Sql)
{
using conn = new SQLConnection(ConnectionString);
{
conn.Open;
using com = new SQLCommand(conn)
{
com.CommandText = sql;
return com.ExecuteReader(CommandBehaviour.CloseConnection);
}
}
}
}

Then to get a reader

Using SQLDataReader reader = MyDB.CreateReader(ConnectionString,SomeSQL)
{
// Do stuff
}

as soon as the above finishes all the stuff goes out of scope and gets disposed as relevant

Because .net implements connection pooling. if you successively execute serveral queries it will try and reuse the connection. You don't have to worry about it.

All your latency issues just disappear, and you aren't keeping things open on the DBMS.

Because ADODB.Net is based on a disconected architecture, you don't need to keep a connection, in fact doing so causes problems.

As you've discovered....

Hths

This discussion is closed to new replies.