473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Not a Question - Anyone tired of guessing types coming from a database?

Try this instead:

// T - the type you are attempting to retrieve.
// o - the data that you want to turn into a T
T get<T>(object o)
{
// handle nulls - turns Nullables into null, reference
types to null and value types to 0.
if (o == null || o == DBNull.Value)
{
return default(T);
}
// we have real data so cast it
else
{
// Gets the underlying nullable type. Null if the type
is not Nullable.
Type t = Nullable.GetUnderlyingType(typeof(T));

// ChangeType is especially nice for dealing with
casts.
// Where explicit casting fails, ChangeType often
succeeds.
// It is dangerous to convert from Object to
Nullable<Udirectly, since Object may not be a U.
// However, from U to Nullable<Ualways succeeds, so
make Object into U first.
// Since we already handled nulls, we need not worry
about casting away null-ness.
return (T)Convert.ChangeType(o, t == null ?
typeof(T) : t);
}
}

This generic method allows you to send in whatever type you desire to
pull off as T. This method will do its best to take what is on the
database and turn it to your type.

It even handles Nullable types so you can put nulls into numbers and
dates.

It will return nulls into the default value of your type, so numbers
become 0, Dates become DateTime.MinValue and reference types become
null.

This is absolutely necessary to provide an efficient, universal and
safe way of managing data types for business objects, especially if
your DBAs like to change precisions on your a lot.

I have been using this code for months now and have been pleasantly
surprised how little I have to worry about guess work anymore.

Let me know if you find any problems with it. Like I said, we have
gone months without any issues at all.

Cheers,
Travis

Oct 19 '07 #1
0 1166

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: אלחנן | last post by:
hi.. i don't know if this is the right group for this.. i have a small application which involves a windows service, and web services which bascally retrieves files, process them and zips them...
5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
6
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
1
by: pamelafluente | last post by:
Hi, While for the various NET types: Boolean, Decimal, ... , String, Char, BYTE , I have seen them sometines appear as a result of a SELECT from a Database, I would like to ask, given a...
1
by: Fregas | last post by:
Just an update to see what O/R Mappers you guys recommend. LINQ is coming out soon but i'd really like to standardize on something at my company. Some O/R Mappers i've already looked at or used...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.