Skip to content

ADO.NET Query Tips in C#

December 15, 2015

Here are some slightly useful tips taken from Christmas Nuggets theme on my other blog mashtheweb.wordpress.com

If you are adding a DataRow item and assigning to an object for a rather complex object multiple times by looping, you know what I mean when you see the code, where objectInfo has these attrbiutes, it is best to write a long variable name when copying and pasting: –

if (dr[“abcdefghijklmnopqrst”] != DBNull.Value)
{
    objectInfo.abcdefghijklmnopqrst = Convert.ToString(dr[“abcdefghijklmnopqrst”]);
}

This will help to identify which variable names are correctly assigned as you may copy one and left it as it is.

 

Following up from Christmas Nuggets #7, it is also useful to use the same principle when copying and pasting lots of attribute properties of an object for an insert into SQL statement like so: –

string query = “INSERT INTO [” + schemaName + “].ObjectInfo” +
“VALUES ( “;
if (ObjectInfo.Name != null)
{
query = query + “‘” + ObjectInfo.Name + “‘,”;
}
else
{
query = query + “”,”;
}
if (ObjectInfo.abcdefghijklmnopqrst != null)
{
query = query + “‘” + ObjectInfo.abcdefghijklmnopqrst + “‘,”;
}
else
{
query = query + “”,”;
}

query = query + ” )”;

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: