Home All Groups Group Topic Archive Search About

disposing the database object??

Author
14 Dec 2006 10:03 AM
Venkatesh
Hi..

i am using the Data Access Application Block of the Enterprise library 2.0.
In the code, i have used:

Database db= DatabaseFactory.CreateDatabase ();

int intRowID;

try

{

string strQuery= "select row_id from Users where login_name='" +
strUserName.Trim() + "'";

intRowID = Convert.ToInt32 (db.ExecuteScalar(CommandType.Text ,strQuery));

return intRowID;

}

catch (Exception ex)

{

throw new Exception(ex.Message);


}

Is it possible to dispose the database object? If yes, how to achieve that?

please help

Author
14 Dec 2006 10:25 AM
Miha Markic [MVP C#]
If Database implements IDisposable then you should call db.Dispose();

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"Venkatesh" <venkatz***@gmail.com> wrote in message
news:342093D6-CA65-4635-9516-8BDA36B17748@microsoft.com...
> Hi..
>
> i am using the Data Access Application Block of the Enterprise library
> 2.0.
> In the code, i have used:
>
> Database db= DatabaseFactory.CreateDatabase ();
>
> int intRowID;
>
> try
>
> {
>
> string strQuery= "select row_id from Users where login_name='" +
> strUserName.Trim() + "'";
>
> intRowID = Convert.ToInt32 (db.ExecuteScalar(CommandType.Text ,strQuery));
>
> return intRowID;
>
> }
>
> catch (Exception ex)
>
> {
>
> throw new Exception(ex.Message);
>
>
> }
>
> Is it possible to dispose the database object? If yes, how to achieve
> that?
>
> please help
>
Author
14 Dec 2006 12:28 PM
ajmastrean
There is no .Dispose() function for the Database object.

The OP did not show the class structure there, but I suppose the
garbage collector will handle disposing that object when he exits the
function or when the entire class is disposed (whatever the case may
be).

If he simply wants to keep his database objects/connections in order,
then there is no real issue. The database object returned by
DatabaseFactory.CreateDatabase() is not responsible for closing
connections, the db.ExecuteScalar() will do that.
Author
14 Dec 2006 2:23 PM
Miha Markic [MVP C#]
"ajmastrean" <ajmastr***@gmail.com> wrote in message
news:1166099283.884550.314080@j72g2000cwa.googlegroups.com...
> There is no .Dispose() function for the Database object.
>
> The OP did not show the class structure there, but I suppose the
> garbage collector will handle disposing that object when he exits the
> function or when the entire class is disposed (whatever the case may
> be).

Only the former is possible while the later isn't.

>
> If he simply wants to keep his database objects/connections in order,
> then there is no real issue. The database object returned by
> DatabaseFactory.CreateDatabase() is not responsible for closing
> connections, the db.ExecuteScalar() will do that.
>
Yes, it is very possible that Database instance doesn't need disposing...
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
Author
15 Dec 2006 6:09 AM
Cor Ligthert [MVP]
Venkatesh,

There are very few objects that needs to be disposed, (those wich contain
not managed *resources*, not to mix up with managed *code*). I cannot see
any non managed resource in your code.

Cor


Show quote
"Venkatesh" <venkatz***@gmail.com> schreef in bericht
news:342093D6-CA65-4635-9516-8BDA36B17748@microsoft.com...
> Hi..
>
> i am using the Data Access Application Block of the Enterprise library
> 2.0.
> In the code, i have used:
>
> Database db= DatabaseFactory.CreateDatabase ();
>
> int intRowID;
>
> try
>
> {
>
> string strQuery= "select row_id from Users where login_name='" +
> strUserName.Trim() + "'";
>
> intRowID = Convert.ToInt32 (db.ExecuteScalar(CommandType.Text ,strQuery));
>
> return intRowID;
>
> }
>
> catch (Exception ex)
>
> {
>
> throw new Exception(ex.Message);
>
>
> }
>
> Is it possible to dispose the database object? If yes, how to achieve
> that?
>
> please help
>
Author
15 Dec 2006 3:11 PM
Ciaran O''Donnell
You code is appalling simply because you are using inline sql vunerable of
sql injection techiniques. Do not put user input directly into your query.
What if the user enters
' Delete from Users
Your use table would be cleared. Use parameters in your SQL or at least
prevent the issue by changing all ' for '' and " for "" to prevent their
interpretation. It is every developers responsibility to write secure code.


Show quote
"Venkatesh" wrote:

> Hi..
>
> i am using the Data Access Application Block of the Enterprise library 2.0.
> In the code, i have used:
>
> Database db= DatabaseFactory.CreateDatabase ();
>
> int intRowID;
>
> try
>
> {
>
> string strQuery= "select row_id from Users where login_name='" +
> strUserName.Trim() + "'";
>
> intRowID = Convert.ToInt32 (db.ExecuteScalar(CommandType.Text ,strQuery));
>
> return intRowID;
>
> }
>
> catch (Exception ex)
>
> {
>
> throw new Exception(ex.Message);
>
>
> }
>
> Is it possible to dispose the database object? If yes, how to achieve that?
>
> please help
>

AddThis Social Bookmark Button