Getting a Specific Object - System.Data.SqlDbType.Int

  • Thread starter Thread starter Vinod I
  • Start date Start date
V

Vinod I

Hi Team,

I am having a string as "System.Data.SqlDbType.Int". Now I want to convert
this string type to actual type to use with my Command object Parameter
Creation. How I will convert this string to the type object ?.


Thanks in advance
 
Dmitriy Lapshin said:
Type myType = Type.GetType("System.Data.SqlDbType.Int");

That won't work for two reasons:

1) System.Data.SqlDbType.Int isn't a type. System.Data.SqlDbType is a
type (an enumeration, in fact) and Int is a member of the enumeration.

2) Type.GetType ("System.Data.SqlDbType") will return null anyway,
because the type lives in System.Data.dll, not mscorlib or the assembly
the code will be running in. You need to specify the full assembly name
to get types from other assemblies.
 
Vinod I said:
Do let me know how to tackle the situation.

Well, we'll need a bit more information. Is it *always* going to be one
of the SqlDbTypes? If not, is there some range it'll be in?
 
Hi,

It will be always of "SqlDBTypes". At runtime I will be getting the type
information as "String" and I want to add parameters to my command object
accordingly. Please let me know the solution as early as possible.

Thanks
 
Vinod I said:
It will be always of "SqlDBTypes". At runtime I will be getting the type
information as "String" and I want to add parameters to my command object
accordingly. Please let me know the solution as early as possible.

In that case, just strip off the bit before the last dot (or just get
your uses to pass "Int" instead) and use
Enum.Parse(typeof(SqlDbTypes), name)
 
That won't work for two reasons:
1) System.Data.SqlDbType.Int isn't a type. System.Data.SqlDbType is a
type (an enumeration, in fact) and Int is a member of the enumeration.

Agree with that. It's me being lazy not to look up in MSDN (I don't use ADO
..NET in the recent months). Apologies to the original poster.
2) Type.GetType ("System.Data.SqlDbType") will return null anyway,
because the type lives in System.Data.dll, not mscorlib or the assembly
the code will be running in. You need to specify the full assembly name
to get types from other assemblies.

I thought that since System.Data.dll was already loaded to the application
domain, type resolution wouldn't need specifying the assembly name. It's
never late to learn!

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
I thought that since System.Data.dll was already loaded to the application
domain, type resolution wouldn't need specifying the assembly name. It's
never late to learn!

It's a pain that there isn't a way of doing basically that. Of course,
System.Data.dll may not have been loaded yet - the assembly isn't (as I
understand it) loaded until it's first actually referenced.

If you try this program:

using System;
using System.Data;
using System.Threading;

class MainClass
{
static void Main(string[] args)
{
Console.WriteLine ("Hello, world.");
Thread.Sleep(5000);
OtherClass.DoSomethingDataRelated();
Thread.Sleep(5000);
}
}

class OtherClass
{
public static void DoSomethingDataRelated()
{
// Quick way of avoiding inlining without
// remembering the attribute name to do it
// properly :)
if (DateTime.Now.Ticks==0)
throw new Exception();

DataSet ds = new DataSet();
}
}

from Visual Studio, watching the Output window, you'll see a 5 second
pause between the start and when the data (and related) assemblies are
loaded.

You can load all referenced assemblies by looking at
Assembly.GetReferencedAssemblies() and loading them recursively. You
could *then* call GetType on each of the assemblies in turn until you
find the right one. Bit grotty though :(
 
Hi,

I think that will solve my problem. But if possible, please let me know
the exact synatax .
I tryed like below. But not working,


objCmd.Parameters.Add(objDRTmp["ParamName"].ToString(),
Enum.Parse(typeof(SqlDbType), "Int"), intSize)

Thank U.
 
Thanks Budy.... I solved the Problem. I have to cast it again, Like,

(SqlDbType) Enum.Parse(typeof(SqlDbType), "Int")


Once Again Thanks for the guidence.



Vinod I said:
Hi,

I think that will solve my problem. But if possible, please let me know
the exact synatax .
I tryed like below. But not working,


objCmd.Parameters.Add(objDRTmp["ParamName"].ToString(),
Enum.Parse(typeof(SqlDbType), "Int"), intSize)

Thank U.
 
Vinod I said:
I think that will solve my problem. But if possible, please let me know
the exact synatax .
I tryed like below. But not working,


objCmd.Parameters.Add(objDRTmp["ParamName"].ToString(),
Enum.Parse(typeof(SqlDbType), "Int"), intSize)

Sorry - you'll need to cast the result of Enum.Parse to SqlDbType:

SqlParameter param = new SqlParameter
(objDRTmp["ParamName"].ToString(),
(SqlDbType)Enum.Parse(typeof(SqlDbType), "Int"),
intSize);

objCmd.Parameters.Add(param);
 
Back
Top