I am trying to consolidate code that is always the same by putting it in a DbManager class. The problem is that I'm not sure if doing this with a SQL Connection object is working properly.
This is the code I'm getting the connection for:
This is the code in the DbManager class that returns a SQL Command object to be used in the previous code:
It seems to work OK, but I think I may have a problem. I'm not sure if the SqlConnection is actually being closed or if I'm leaving it open out there. There are lots of methods for accessing data and I was just trying to consolidate some of the data access code into a wrapper type class.
Does getting the connection for the command this way leave a connection out there. If I'm working on lots of records at the same time, I can get a SQL Exception (time out or connection pool problems).
Can anyone tell me if this actually closes the connection or not?
Thanks,
Ron
This is the code I'm getting the connection for:
C#:
[COLOR=blue]public[/COLOR] [COLOR=blue]bool[/COLOR] CheckIfChildRecordsExist([COLOR=blue]string[/COLOR] tableName, [COLOR=blue]string[/COLOR] idNameValue, [COLOR=blue]string[/COLOR] idName)
{
[COLOR=blue]bool[/COLOR] exists [COLOR=#800040]=[/COLOR] [COLOR=blue]true[/COLOR]; [COLOR=green]// default to true so we don't inadvertantly try to delete child records...[/COLOR]
[COLOR=#2b91af]StringBuilder[/COLOR] query [COLOR=#800040]=[/COLOR] [COLOR=blue]new[/COLOR] [COLOR=#2b91af]StringBuilder[/COLOR]();
[COLOR=blue]string[/COLOR] paramName [COLOR=#800040]=[/COLOR] [COLOR=#a31515]"@"[/COLOR] [COLOR=#800040]+[/COLOR] idName;
[COLOR=blue]int[/COLOR] recordCount [COLOR=#800040]=[/COLOR] [COLOR=#5f5f5f]0[/COLOR];
query[COLOR=#800040].[/COLOR]Append([COLOR=#a31515]"SELECT COUNT(*) FROM "[/COLOR]);
query[COLOR=#800040].[/COLOR]Append(tableName);
query[COLOR=#800040].[/COLOR]Append([COLOR=#a31515]" WHERE "[/COLOR]);
query[COLOR=#800040].[/COLOR]Append(idName);
query[COLOR=#800040].[/COLOR]Append([COLOR=#a31515]" = "[/COLOR]);
query[COLOR=#800040].[/COLOR]Append(paramName);
[COLOR=blue]using[/COLOR] ([COLOR=#2b91af]SqlCommand[/COLOR] cmd [COLOR=#800040]=[/COLOR] [COLOR=blue]new[/COLOR] [COLOR=#2b91af]DBManager[/COLOR]()[COLOR=#800040].[/COLOR]GetDbSqlQueryCommand(query[COLOR=#800040].[/COLOR]ToString()))
{
cmd[COLOR=#800040].[/COLOR]Parameters[COLOR=#800040].[/COLOR]AddWithValue(paramName, idNameValue);
cmd[COLOR=#800040].[/COLOR]Connection[COLOR=#800040].[/COLOR]Open();
recordCount [COLOR=#800040]=[/COLOR] ([COLOR=blue]int[/COLOR])cmd[COLOR=#800040].[/COLOR]ExecuteScalar();
cmd[COLOR=#800040].[/COLOR]Connection[COLOR=#800040].[/COLOR]Close();
}
[COLOR=blue]if[/COLOR] (recordCount [COLOR=#800040]>[/COLOR] [COLOR=#5f5f5f]0[/COLOR])
{
exists [COLOR=#800040]=[/COLOR] [COLOR=blue]true[/COLOR];
}
[COLOR=blue]else[/COLOR]
{
exists [COLOR=#800040]=[/COLOR] [COLOR=blue]false[/COLOR];
}
[COLOR=blue]return[/COLOR] exists;
}
This is the code in the DbManager class that returns a SQL Command object to be used in the previous code:
C#:
[COLOR=blue]public[/COLOR] [COLOR=#2b91af]SqlCommand[/COLOR] GetDbSqlQueryCommand([COLOR=blue]string[/COLOR] query)
{
[COLOR=#2b91af]SqlCommand[/COLOR] cmd [COLOR=#800040]=[/COLOR] [COLOR=blue]new[/COLOR] [COLOR=#2b91af]SqlCommand[/COLOR](query, [COLOR=blue]new[/COLOR] [COLOR=#2b91af]SqlConnection[/COLOR](ConnectionString));
cmd[COLOR=#800040].[/COLOR]CommandType [COLOR=#800040]=[/COLOR] System[COLOR=#800040].[/COLOR]Data[COLOR=#800040].[/COLOR][COLOR=#2b91af]CommandType[/COLOR][COLOR=#800040].[/COLOR]Text;
[COLOR=blue]return[/COLOR] cmd;
}
It seems to work OK, but I think I may have a problem. I'm not sure if the SqlConnection is actually being closed or if I'm leaving it open out there. There are lots of methods for accessing data and I was just trying to consolidate some of the data access code into a wrapper type class.
Does getting the connection for the command this way leave a connection out there. If I'm working on lots of records at the same time, I can get a SQL Exception (time out or connection pool problems).
Can anyone tell me if this actually closes the connection or not?
Thanks,
Ron