473,662 Members | 2,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.net types to sqldbtypes

Roy
The SqlCommand.Para meters.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 5346
On Fri, 03 Mar 2006 13:11:28 +0800, Roy <Ro*@discussion s.microsoft.com >
wrote:
The SqlCommand.Para meters.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.VarCh ar);
cmd.Parameters. Add("value", SqlDbType.VarCh ar);
cmd.Parameters. Add("enabled", SqlDbType.Bit);
cmd.Parameters. Add("isCollecti on", 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.ConvertToStr ing(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollecti on"].Value = false;

No casting between types is required
Mar 3 '06 #2
Hi,
"Roy" <Ro*@discussion s.microsoft.com > wrote in message
news:45******** *************** ***********@mic rosoft.com...
The SqlCommand.Para meters.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.Va lue 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.Para meters.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*@discussion s.microsoft.com >
wrote:
The SqlCommand.Para meters.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.VarCh ar);
cmd.Parameters. Add("value", SqlDbType.VarCh ar);
cmd.Parameters. Add("enabled", SqlDbType.Bit);
cmd.Parameters. Add("isCollecti on", 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.ConvertToStr ing(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollecti on"].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.Para meters.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*@discussion s.microsoft.com > wrote in message
news:D2******** *************** ***********@mic rosoft.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.Para meters.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*@discussion s.microsoft.com >
wrote:
> The SqlCommand.Para meters.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.VarCh ar);
cmd.Parameters. Add("value", SqlDbType.VarCh ar);
cmd.Parameters. Add("enabled", SqlDbType.Bit);
cmd.Parameters. Add("isCollecti on", 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.ConvertToStr ing(value);
cmd.Parameters["enabled"].Value = true;
cmd.Parameters["isCollecti on"].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
4536
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, what does the acronym stand for? Thanks, </dib> John Dibling Witty banter omitted for your protection
5
3446
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++ standard namespace. std::uint8_t instead of uint8_t std::uint32_t instead of uint32_t .... I don't think the use of std:: is correct. Nowhere are these types
8
2328
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 least 2 bytes, and a long at least 4 bytes. The rules also define a size precedence to the types. In Stroustrup's book, it says that all type sizes are multiples of char, "so by definition the size of a char is 1." According to the rules, that...
188
17316
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
2094
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
58
3445
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 have said wrong. Keep in mind that this is a tutorial, and I donot want to overwhelm the reader with little details. -------------------------------------------------------------------------* Types
14
2300
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 whateverkeithsaysfor99compliance #define whateverkeithsays for99compliance int main(void { struct foo
3
2382
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 expressions
55
3962
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 built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8435
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.