473,385 Members | 2,162 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,385 software developers and data experts.

.net types to sqldbtypes

Roy
The SqlCommand.Parameters.Add(..) function takes an paramter for SqlDBType.
Is there a generic way in .NET framework to convert .net types to SqlDBTypes?
Or I have to mapping manually with a big switch statement?
Mar 3 '06 #1
5 5337
On Fri, 03 Mar 2006 13:11:28 +0800, Roy <Ro*@discussions.microsoft.com>
wrote:
The SqlCommand.Parameters.Add(..) function takes an paramter for
SqlDBType.
Is there a generic way in .NET framework to convert .net types to
SqlDBTypes?
Or I have to mapping manually with a big switch statement?

Conversion is handled automatically for you, for example:
cmd.Parameters.Add("control_id", SqlDbType.Int);
cmd.Parameters.Add("style_id", SqlDbType.Int);
cmd.Parameters.Add("name", SqlDbType.VarChar);
cmd.Parameters.Add("value", SqlDbType.VarChar);
cmd.Parameters.Add("enabled", SqlDbType.Bit);
cmd.Parameters.Add("isCollection", SqlDbType.Bit);

cmd.Parameters["control_id"].Value = controlId;
cmd.Parameters["style_id"].Value = _styleId;
cmd.Parameters["name"].Value = property.Name;
cmd.Parameters["value"].Value = tc.ConvertToString(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollection"].Value = false;

No casting between types is required
Mar 3 '06 #2
Hi,
"Roy" <Ro*@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
The SqlCommand.Parameters.Add(..) function takes an paramter for
SqlDBType.
Is there a generic way in .NET framework to convert .net types to
SqlDBTypes?
Or I have to mapping manually with a big switch statement?


It does it for you , SqlParameter.Value is of type object so it will do the
conversion internally.

One very important clarification though. if you are passing a null you have
to use DBNull , not null.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 3 '06 #3
Roy
My situation is I have parameters as object array. When I walk through the
array, I can get .NET data types for each parameter. However,
SqlCommand.Parameters.Add(..) function
takes SqlDBType as its parameter. I can do a mapping with a switch
statement. but I want to know if there is a generic way or existing class
provided by .NET to do the mapping.

"Mark Harris" wrote:
On Fri, 03 Mar 2006 13:11:28 +0800, Roy <Ro*@discussions.microsoft.com>
wrote:
The SqlCommand.Parameters.Add(..) function takes an paramter for
SqlDBType.
Is there a generic way in .NET framework to convert .net types to
SqlDBTypes?
Or I have to mapping manually with a big switch statement?

Conversion is handled automatically for you, for example:
cmd.Parameters.Add("control_id", SqlDbType.Int);
cmd.Parameters.Add("style_id", SqlDbType.Int);
cmd.Parameters.Add("name", SqlDbType.VarChar);
cmd.Parameters.Add("value", SqlDbType.VarChar);
cmd.Parameters.Add("enabled", SqlDbType.Bit);
cmd.Parameters.Add("isCollection", SqlDbType.Bit);

cmd.Parameters["control_id"].Value = controlId;
cmd.Parameters["style_id"].Value = _styleId;
cmd.Parameters["name"].Value = property.Name;
cmd.Parameters["value"].Value = tc.ConvertToString(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollection"].Value = false;

No casting between types is required

Mar 3 '06 #4
> My situation is I have parameters as object array. When I walk through the
array, I can get .NET data types for each parameter. However,
SqlCommand.Parameters.Add(..) function
takes SqlDBType as its parameter. I can do a mapping with a switch
statement. but I want to know if there is a generic way or existing class
provided by .NET to do the mapping.
You've got a big problem there. There are far more SqlDBTypes than there are
matching .Net data types. The same .Net data types are used for multiple
SqlDBTypes, so it's not possible to determine the SqlDBType for a given .Net
data type. You can work it the other way round, but not that way.

As an example, take the .Net data type String. It is mapped to the following
SqlDBTypes:

Char
NChar
NText
NVarChar
Text
VarChar

Now, if you know the SqlDBType is one of these, you know that it is mapped
to System.String. But if you know the data type of a piece of data is a
string, which of these SqlDBTypes should it be matched to?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"Roy" <Ro*@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com... My situation is I have parameters as object array. When I walk through the
array, I can get .NET data types for each parameter. However,
SqlCommand.Parameters.Add(..) function
takes SqlDBType as its parameter. I can do a mapping with a switch
statement. but I want to know if there is a generic way or existing class
provided by .NET to do the mapping.

"Mark Harris" wrote:
On Fri, 03 Mar 2006 13:11:28 +0800, Roy <Ro*@discussions.microsoft.com>
wrote:
> The SqlCommand.Parameters.Add(..) function takes an paramter for
> SqlDBType.
> Is there a generic way in .NET framework to convert .net types to
> SqlDBTypes?
> Or I have to mapping manually with a big switch statement?

Conversion is handled automatically for you, for example:
cmd.Parameters.Add("control_id", SqlDbType.Int);
cmd.Parameters.Add("style_id", SqlDbType.Int);
cmd.Parameters.Add("name", SqlDbType.VarChar);
cmd.Parameters.Add("value", SqlDbType.VarChar);
cmd.Parameters.Add("enabled", SqlDbType.Bit);
cmd.Parameters.Add("isCollection", SqlDbType.Bit);

cmd.Parameters["control_id"].Value = controlId;
cmd.Parameters["style_id"].Value = _styleId;
cmd.Parameters["name"].Value = property.Name;
cmd.Parameters["value"].Value = tc.ConvertToString(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollection"].Value = false;

No casting between types is required

Mar 3 '06 #5
Agreed... The data type argument to Parameters.Add() is governed by the
database field data type, not the .NET data type used to contain the data.
Mar 3 '06 #6

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

Similar topics

3
by: John Dibling | last post by:
Could somebody please direct me to a location in the standard where POD types are defined? That is, where in the standard is it defined what attributes a POD type has that a non-POD hasn't? Also,...
5
by: Andy Skypeck | last post by:
I am looking for some validation against a dubious coding practice that prevails where I work. C types defined in types.h (Linux) or stdint.h (Windows, C99?) are used as if they belong to the C++...
8
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
58
by: jacob navia | last post by:
Hi people I have been working again in my tutorial, and I have finished the "types" chapter. If you feel like "jacob bashing" this is the occasion! I am asking for criticisms, or for things I may...
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
3
by: sophia.agnes | last post by:
Dear all, what are the major expression types in c? i have seen the following types of expressions 1) constant expressions 2) integral expressions 3) float expressions 4) pointer...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.