|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
disposing the database object??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 If Database implements IDisposable then you should call db.Dispose();
-- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "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 > 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. "ajmastrean" <ajmastr***@gmail.com> wrote in message Only the former is possible while the later isn't.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). > Yes, it is very possible that Database instance doesn't need disposing...> 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. > -- 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/ 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 > 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 > |
|||||||||||||||||||||||