.Net 2.0 Autogenerate update command parameters from a disconnected datatable

D

david00001

I am trying to find code to generate the command parameters for
updating a datatable (of changes received from UI layer) in a middle
layer object. I want to use a dataadaptor to do the update.
I can easily generate the update, insert and delete sqls for the
command objects, but there seems to be a complete disconnect between
the column datatype info available from the datatable and the
Sqlparameter objects required to create the command objects.

For example, I know the source column name from the database that I
want to update back to, and I know what column in the datatable is the
data required. How do I work out that the string column from the
datatable requires a SqlDbType.VarChar with length of 60 ie varchar(60)
without going back to the database and querying system tables? Surely
the info must be availble somewhere in the datatable schema? How do I
get it?
This is very frustrating. All the sample code I can find is either the
usual kiddy code that assumes you only do simple things with these very
complex objects or so abstract that its useless to someone of
relatively normal intelligence.

Any help would be very greatly appreciated.
Thankyou
 
M

Matt Noonan

david00001 said:
I am trying to find code to generate the command parameters for
updating a datatable (of changes received from UI layer) in a middle
layer object. I want to use a dataadaptor to do the update.
I can easily generate the update, insert and delete sqls for the
command objects, but there seems to be a complete disconnect between
the column datatype info available from the datatable and the
Sqlparameter objects required to create the command objects.

For example, I know the source column name from the database that I
want to update back to, and I know what column in the datatable is the
data required. How do I work out that the string column from the
datatable requires a SqlDbType.VarChar with length of 60 ie
varchar(60) without going back to the database and querying system
tables? Surely the info must be availble somewhere in the datatable
schema? How do I get it?
This is very frustrating. All the sample code I can find is either the
usual kiddy code that assumes you only do simple things with these
very complex objects or so abstract that its useless to someone of
relatively normal intelligence.

You might check into a code generator, where the metadata is provided for
you and you can write your own custom templates.

http://www.mygenerationsoftware.com
 
R

Robbe Morris [C# MVP]

Check the link in my signature below. Makes it much
easier to use an OOP approach.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/adonet_source_code_generator.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top