lamp

February 2010 - Posts

A generic way to call ExecuteScalar
05 February 10 07:16 AM | Frederick.Chapleau | with no comments

When calling execute Scalar, we always begin by calling it with some code like

int i = cmd.ExecuteScalar();

What’s wrong with this picture?… what if, the first field of the first row is… NULL?

so…

int i;
object o = cmd.ExecuteScalar();
if(o != null)
    i = (int)o;

-f.

Filed under:
Multi Line string in C#
02 February 10 10:57 AM | Frederick.Chapleau | with no comments

Ever wonder of a more efficient way to write multiple lines strings?

Check this out…

The first way...

string myString1 = "This is the first line of my string.\n" +
                   "This is the second line of my string.\n" +
                   "This is the third line of the string.\n";
And a more efficient way...

string myString2 = @"This is the first line of my string.
This is the second line of my string.
This is the third line of the string.";

-f.

Filed under:
Usage of static constant for SQL Statements
01 February 10 01:01 PM | Frederick.Chapleau | with no comments

Embedding SQL statement is sometimes useful. A good practice is to centralize them in a class, using public constants...

public const string SQL_MYTABLE_INSERT = "INSERT MYTABLE (FIELD) VALUES (@FIELDVALUE)";

-f.

Filed under: