I am not sure about the Private/Public scope of this method, someone else will have to talk about that. but you can return inside a using statement, so instead of this:
private String GetString(long index)
{
String output;
using (SqlCommand command = new SqlCommand(CommandText, Connection))
{
command.Connection.Open();
output = command.ExecuteScalar() as String;
command.Connection.Close();
}
return output;
}
you would just return like this
private String GetString(long index)
{
using (SqlCommand command = new SqlCommand(CommandText, Connection))
{
command.Connection.Open();
return (command.ExecuteScalar()).ToString();
}
}
Assuming thatThe Command is automatically taken care of so you don't have to worry about explicitly disposing of it returns a string.
BecauseI assume that the connection is opened and closed by the piece of code that calls this private method, so I left out the connection open and close.
I am kind of on edge about doing it that way, but it seems like you are using awould only want to open one connection for all the usingSqlCommand
statements that you don't need to explicitly close the connection, it is done automagically whenrun during the using block is exited regardlesscourse of how it is donethe application run.