We need to use either “using” or explicitly close the connection if we use the datareader.
Otherwise, it will lead to Connection Pool error. See the underlined text below:
However, there are other cases where it is unclear when to close the connection. An example is the ExecuteReader method. This method returns an object that implements the IDataReader interface. The Database base class has a default implementation that returns a DbDataReader object. DbDataReader objects are designed to read specific portions of the data as needed, which requires an open connection. In other words, it is unknown when the application no longer needs the DbDataReader. If the Data Access Application Block methods close the connection before returning the DbDataReader, the DbDataReader becomes useless to the client code. Instead, the DbDataReader methods indicate to the underlying ADO.NET call to automatically close the connection when the DbDataReader is finished. In this situation, it is considered a best practice for the application to ensure that the DbDataReader is closed in a timely fashion, either by explicitly closing the reader with the DbDataReader.close method or by forcing the disposal of the DbDataReader, which results in the Close method being called.
The following code demonstrates a call to the ExecuteReader method. The using statement (Using in Visual Basic) ensures that the DbDataReader object is disposed, which closes the DbDataReader object.
[C#]
Copy Code
Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCommand = db.GetSqlStringCommand("Select Name, Address From Customers");
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
// Process results
}
when u are creating a control, and u decides that a property or a event should not be displayed in property window.
System.ComponentModel.BrowsableAttribute is there for you, to do the above.
Eg:
private string text;
[Browsable(false)] // True is default public string Text { get { return text; } set { text = value; } }
In a Visual Studio 2008 source window, right-click and select Organize Usings | Remove Unused Usings. Visual Studio 2005 users may use ReSharper for this.
P.S. interesting, does this optimization gives you any benefits other than cleaner code?
I was wondering how to capitalize properties which have either ID or OK abbreviation in its name. For example, is it TipID or TipId? I have found the answer in Design Guidelines for Developing Class Libraries:
In Pascal-cased identifiers (all identifiers except parameter names) they should appear as Id, and Ok. If used as the first word in a camel-cased identifier, they should appear as id and ok, respectively.
P.S. And of course .NET Framework doesn't fulfil this rule :) For example, Control.ClientID property.