Infer SqlDbType Enumeration Member via Reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett  Kelly

    Infer SqlDbType Enumeration Member via Reflection

    Hello all,

    I'm in a situation where I need to retrieve a member from the
    System.Data.Sql DbType enumeration knowing only the type name.

    At this point, I'm just trying to get reflection to work... This is
    what I've got so far:

    using System;
    using System.Reflecti on;
    using System.Data;

    namespace PleaseFreakinWo rk
    {
    public class TypeTest
    {
    public static void Main(string[] args)
    {
    try
    {
    Type MyType = Type.GetType("S ystem.Data.SqlD bType");
    Console.WriteLi ne("Type Name:" + MyType.Name);
    }
    catch(Exception ex)
    {
    Console.WriteLi ne(ex.Message);
    Console.WriteLi ne(ex.StackTrac e);
    }
    }
    }
    }

    And when I run this, it blows up on the Console.WriteLi ne call w/
    "Object ref not set to an instance of an object". Now, if I change the
    type name to System.Reflecti on.Assembly, it works fine. But if I
    change it to System.Data.Sql Client.SqlConne ction (for example), that
    doesn't work.

    I've tried loading the System.Data.dll assembly, but it errors out
    saying that the file or one of its dependencies could not be found...

    Help!

    TIA,

    Brett

  • Jon Skeet [C# MVP]

    #2
    Re: Infer SqlDbType Enumeration Member via Reflection

    Brett Kelly <inkedmn@gmail. com> wrote:[color=blue]
    > At this point, I'm just trying to get reflection to work... This is
    > what I've got so far:[/color]

    <snip>
    [color=blue]
    > And when I run this, it blows up on the Console.WriteLi ne call w/
    > "Object ref not set to an instance of an object". Now, if I change the
    > type name to System.Reflecti on.Assembly, it works fine. But if I
    > change it to System.Data.Sql Client.SqlConne ction (for example), that
    > doesn't work.[/color]

    Yup. That's because System.Reflecti on.Assembly is in mscorlib, but
    SqlConnection isn't. If you don't provide an assembly name in the type
    name, only mscorlib and the currently executing assembly are searched.

    To find what to use for a particular type, use typeof(...) in a test
    program and write out the AssemblyQualifi edName property. For example,
    for SqlConnection it's:

    System.Data.Sql Client.SqlConne ction, System.Data, Version=1.0.500 0.0,
    Culture=neutral , PublicKeyToken= b77a5c561934e08 9

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Martin Robins

      #3
      Re: Infer SqlDbType Enumeration Member via Reflection

      I could be off base here; but am I reading correctly that you want to be
      able to retrieve the enumeration value based on the name of the member, ie
      given the string "VarChar", you want to set a value to SqlDbType.Varch ar?

      If this is the case; SqlDbType value =
      (SqlDbType)Enum .Parse(typeof(S qlDbType), "VarChar", true); will achieve it.
      If not, please ignore me.


      "Brett Kelly" <inkedmn@gmail. com> wrote in message
      news:1118969961 .744461.179370@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > Hello all,
      >
      > I'm in a situation where I need to retrieve a member from the
      > System.Data.Sql DbType enumeration knowing only the type name.
      >
      > At this point, I'm just trying to get reflection to work... This is
      > what I've got so far:
      >
      > using System;
      > using System.Reflecti on;
      > using System.Data;
      >
      > namespace PleaseFreakinWo rk
      > {
      > public class TypeTest
      > {
      > public static void Main(string[] args)
      > {
      > try
      > {
      > Type MyType = Type.GetType("S ystem.Data.SqlD bType");
      > Console.WriteLi ne("Type Name:" + MyType.Name);
      > }
      > catch(Exception ex)
      > {
      > Console.WriteLi ne(ex.Message);
      > Console.WriteLi ne(ex.StackTrac e);
      > }
      > }
      > }
      > }
      >
      > And when I run this, it blows up on the Console.WriteLi ne call w/
      > "Object ref not set to an instance of an object". Now, if I change the
      > type name to System.Reflecti on.Assembly, it works fine. But if I
      > change it to System.Data.Sql Client.SqlConne ction (for example), that
      > doesn't work.
      >
      > I've tried loading the System.Data.dll assembly, but it errors out
      > saying that the file or one of its dependencies could not be found...
      >
      > Help!
      >
      > TIA,
      >
      > Brett
      >[/color]


      Comment

      • Brett  Kelly

        #4
        Re: Infer SqlDbType Enumeration Member via Reflection

        Sir, that is *precisely* what I was after :)

        Thanks!

        Martin Robins wrote:[color=blue]
        > I could be off base here; but am I reading correctly that you want to be
        > able to retrieve the enumeration value based on the name of the member, ie
        > given the string "VarChar", you want to set a value to SqlDbType.Varch ar?
        >
        > If this is the case; SqlDbType value =
        > (SqlDbType)Enum .Parse(typeof(S qlDbType), "VarChar", true); will achieve it.
        > If not, please ignore me.
        >
        >
        > "Brett Kelly" <inkedmn@gmail. com> wrote in message
        > news:1118969961 .744461.179370@ f14g2000cwb.goo glegroups.com.. .[color=green]
        > > Hello all,
        > >
        > > I'm in a situation where I need to retrieve a member from the
        > > System.Data.Sql DbType enumeration knowing only the type name.
        > >
        > > At this point, I'm just trying to get reflection to work... This is
        > > what I've got so far:
        > >
        > > using System;
        > > using System.Reflecti on;
        > > using System.Data;
        > >
        > > namespace PleaseFreakinWo rk
        > > {
        > > public class TypeTest
        > > {
        > > public static void Main(string[] args)
        > > {
        > > try
        > > {
        > > Type MyType = Type.GetType("S ystem.Data.SqlD bType");
        > > Console.WriteLi ne("Type Name:" + MyType.Name);
        > > }
        > > catch(Exception ex)
        > > {
        > > Console.WriteLi ne(ex.Message);
        > > Console.WriteLi ne(ex.StackTrac e);
        > > }
        > > }
        > > }
        > > }
        > >
        > > And when I run this, it blows up on the Console.WriteLi ne call w/
        > > "Object ref not set to an instance of an object". Now, if I change the
        > > type name to System.Reflecti on.Assembly, it works fine. But if I
        > > change it to System.Data.Sql Client.SqlConne ction (for example), that
        > > doesn't work.
        > >
        > > I've tried loading the System.Data.dll assembly, but it errors out
        > > saying that the file or one of its dependencies could not be found...
        > >
        > > Help!
        > >
        > > TIA,
        > >
        > > Brett
        > >[/color][/color]

        Comment

        Working...