views:

1249

answers:

3

If say I need to run two separate SQL statements against two separate databases. Right now I do (pseudocode):

Try{

declare variable connectionA to DatabaseA 
declare variable connectionB to DatabaseB 

connectionA.open()
connectionB.open() 


declare variable SQLCmdA with ConnectionA and one SQL statement 
declare variable SQLCmdB with ConnectionB and another SQL statement 

SQLCmdA.executeNonQuery() 
SQLCmdB.executeNonQuery()

}

Catch () 
{
   print error message
}

Finally(){ 
  connectionA.close() 
  connectionB.close()
  SQLCmdA.Dispose()
  SQLCmdB.Dispose() 
}

The above seems very clumsy. And if I have three different sql statements, i would need three different SQLCmd variables.

Is there a "standard" way of doing such things, especially in terms of efficiency, performance? if anyone can provide a simple improved pseudocode, that'd be great.

In addition, do I need to worry about implementing Connection Pooling, to conserve resource and speed up the program? If so, how do I implement it in this case?

Thanks!

+1  A: 

If you need all two (or three, or...) connetions open at the same time and need to retain the SqlCommand for each of them, then yes, you're probably going to have to do it the way you're doing it.

However, if you only need one connection open at a time, you could use a single connection and single command, then change things as needed.

Michael Todd
Hmm ok. I don't need two connections open at the same time. So I will retain one connection variable and one sqlCMD variable, and sequentially reassign them to different databases.So if I need to run SQL statement 1 against database 1, then SQL statement 2 against database 2, then SQL statement 1 against database 1 again. I need to reassign the connection and cmdSQL variables three times?
Saobi
That's correct. the SqlCommand.Connection property and the SqlCommand.CommandText property would need to be changed each time. You could, for example, put each of those in an array and then use a for loop to programmatically change things (rather than duplicating the code changes three times).
Michael Todd
+1  A: 

If you're going to be doing low-level database access, this seems fine to me. Of course, if you only need one database connection open at any time, you could abstract most of the code into a method (that takes an SQL command/text as a parameter and returns the result), but this may not be the case in your situation.

You could also make things slightly neater by making use of using statements, as such:

using(var sqlConnectionA = new ...)
using(var sqlConnectionB = new ...)
{
    try
    {
        // Perform queries here.
    }
    catch (SqlException exSql)
    {
        // SQL error
    }
}
Noldorin
+1  A: 

Instead of adding variables, why not make a class?

public class MyDatabaseConnection {
    public MyDatabaseConnection(string connectionString) {
        this.connectionString = connectionString;
        // create a database connection perhaps
    }
    // some methods for querying a database
    public void execute(string query) { }
}

In this case it's easy to add a third database connection

MyDatabaseConnection con1 = new MyDatabaseConnection("Server=localhost");
MyDatabaseConnection con2 = new MyDatabaseConnection("Server=other_server");
MyDatabaseConnection con3 = new MyDatabaseConnection("Server=third_one");

And execute an sql query on each

MyDatabaseConnection[] cons = new MyDatabaseConnection[]{ con1, con2, con3 };
foreach (MyDatabaseConnection con in cons) {
    con.execute(someSqlCommandText);
}
Patrick