[
Advertise | Submit Code | About us | Contact us | Link us
]
Go!
Membership Services
Login
Register

Home
C# General

General

C# Language

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting
C# Windows Forms

General

Combo and List boxes

Miscellaneous Controls

Button Controls

Edit Controls
Cutting Edge

ASP.NET 2.0

Visual Studio 2005

Windows Longhorn

SQL Server 2005
C# Multimedia and GDI+

General

DirectX

GDI+

Audio
Internet & Web

General

Images and multimedia

Database

Utilities

Security

ASP.NET Controls

Design and Architecture

Webservices
.NET

General

Design & Architecture

Algorithms

Database

Security

Active Directory

COM Interop

Remoting

ADO.NET

XML.NET

Tools

Enterprise

IDE
Visual Basic .NET

VB.NET General

VB.NET Controls
General Reading

.NET Books Review

Product Showcase

Book Chapters

Business Design & Strategy
Community

Discuss

Job Board

Discussion

CodeXchange
DeveloperLand

Advertise

Submit Code

About us

Contact us

Link us
Miscellaneous

Favorite Links

Downloads

Programming Sites

Top Stories
Regular Expressions

E-Mail

Date/Time
Home > Visual Basic .NET > VB.NET General
Using the DataLinks object without including any additional references
Posted by on Tuesday, July 19, 2005 (EST)

This article is going to discuss two ways of displaying the DataLinks dialog to facilitate user entry of connection strings. The code you will see here is from a minimalist’s perspective, since I myself like to write as few lines and include as little code as possible to get the job done

This article has been viewed: 5,115 times
Technology: VB.NET General.

ConnectionStringBuilding.zip (8.43 KB)

Contents

Introduction

This article is going to discuss two ways of displaying the DataLinks dialog to facilitate user entry of connection strings.  The code you will see here is from a minimalist’s perspective, since I myself like to write as few lines and include as little code as possible to get the job done.  I will show two approaches available to VB.net developers to include the DataLinks dialog without the need include any additional references. (aaaha you guessed it late-binding)

Most applications today use some means of persisting data, and to this end the relational database is the method of choice.  DotNet developers know that in order to anything with a relational database you must first be able to connect to that database.

The DataLinks object is a COM component.  So you can, if you want, create interop stubs through Visual Studio or manually through TlbImp.exe.  Doing one of these makes it easy to write code that makes use of the COM component, however in order to keep my code as thin as possible I chose not include any references nor add any interop stub files to my project. Instead, the DataLinks component is created through late-binding and methods are invoked via reflection.

As an additional exercise I thought it would be useful to demonstrate the two types of reflection available to VB.net developers. 

VB6 developers should be familiar with CreateObject and CallByName functions.  These functions gave VB6 developers the flexibility to create objects and invoke methods using strings.  Microsoft made the decision to keep these functions as part of the .Net framework so they can be used to perform dynamic invocation. With .Net came a very powerful reflection api.  Through the use of various classes in the System.Reflection namespace and the Type and Activator classes, these same operations (and more) can be performed.

The code is short and pretty much self explanatory.  So without further adieu:

Top Go to Table of Contents

Code

Private Sub VB6StyleReflection()
        Dim dlink As Object = CreateObject("Datalinks")
        CallByName(dlink, "hWnd", CallType.Let, Me.Handle.ToInt32)
        If txtConnectionString.Text = String.Empty Then
            Dim conn As Object = CallByName(dlink, "PromptNew", CallType.Method)
            If Not Object.Equals(conn, Nothing) Then
                txtConnectionString.Text = CType(CallByName(conn, "ConnectionString", CallType.Get), String)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(conn)
            End If
        Else
            Dim conn As Object = CreateObject("ADODB.Connection")
            CallByName(conn, "ConnectionString", CallType.Let, txtConnectionString.Text)
            If CType(CallByName(dlink, "PromptEdit", CallType.Method, conn), Boolean) Then
                txtConnectionString.Text = CType(CallByName(conn, "ConnectionString", CallType.Get), String)
            End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(conn)
        End If
        System.Runtime.InteropServices.Marshal.ReleaseComObject(dlink)
End Sub
Private Sub DotNetStyleReflection()
        Dim dlinkType As Type = Type.GetTypeFromProgID("Datalinks", True)
        Dim dlink As Object = Activator.CreateInstance(dlinkType)
        dlinkType.InvokeMember("hWnd", BindingFlags.SetProperty Or 
         BindingFlags.Public Or BindingFlags.Instance, 
         Nothing, 
         dlink, 
         New Object() {Me.Handle.ToInt32})
        If txtConnectionString.Text = String.Empty Then
            Dim conn As Object = dlinkType.InvokeMember("PromptNew", 
             BindingFlags.InvokeMethod Or BindingFlags.Public Or BindingFlags.Instance , 
              Nothing, 
              dlink, 
              Nothing)
            If Not Object.Equals(conn, Nothing) Then
                Dim connType As Type = conn.GetType()
                txtConnectionString.Text = DirectCast(connType.InvokeMember("ConnectionString", 
                 BindingFlags.GetProperty Or BindingFlags.Instance Or BindingFlags.Public, 
                 Nothing, 
                 conn, 
                 Nothing), String)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(conn)
            End If
        Else
            Dim connType As Type = Type.GetTypeFromProgID("ADODB.Connection", True)
            Dim conn As Object = Activator.CreateInstance(connType)
            connType.InvokeMember("ConnectionString", 
             BindingFlags.SetProperty Or BindingFlags.Instance Or BindingFlags.Public, 
             Nothing, 
             conn, 
             New Object() {txtConnectionString.Text})
            If DirectCast(dlinkType.InvokeMember("PromptEdit", 
            BindingFlags.InvokeMethod Or BindingFlags.Public Or BindingFlags.Instance, 
             Nothing, 
             dlink, New Object() {conn}), 
             Boolean) Then
                 txtConnectionString.Text = DirectCast(connType.InvokeMember("ConnectionString", 
                  BindingFlags.GetProperty Or BindingFlags.Instance Or 
                  BindingFlags.Public, 
                  Nothing, 
                  conn, 
                  Nothing), String)
            End If
            System.Runtime.InteropServices.Marshal.ReleaseComObject(conn)
        End If
        System.Runtime.InteropServices.Marshal.ReleaseComObject(dlink)
End Sub

Top Go to Table of Contents

Conclusion

Obviously instantiating COM objects this way is going to save you from having to declare the reference, but for that bit it buys you there is a price.  The costs are dynamic invocation is more error prone, and executes more slowly than early bound code.  In this case I think these costs are acceptable because a little additional testing can flush out any issues in this small bit of code, and since this is a UI component that would likely not be used frequently a few extra tenths of a second will likely not be an issue.

Top Go to Table of Contents

Refrences

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vbconcallingpropertyormethodusingstringname.asp [^] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemreflection.asp [^]

Top Go to Table of Contents

Connection String Information:

http://www.connectionstrings.com/ [^] http://www.sqlstrings.com/ [^]

http://www.carlprothman.net/Default.aspx?tabid=81 [^]

Top Go to Table of Contents

About Kevin Sprague

I have been working as a consultant in the Ohio area since 2002. My primary areas of expertise are .Net, Web Services, Java, Sql Server, and Oracle. I am a programming junkie. I do it all day on the client site and then go home and do some more!

Click here if you want to know more about .

Other articles that may interest you

  • Write a Word Add-In – Part 0
  • Write a Word Add-In – Part I
  • Lengthy Operations on Single Thread in .NET Application
  • Learning Draughts
  • Exceptions and Performance
  • Average Rating :

    Discussion Forums
    Got a programming related question? Hopefully someone has the answer... Want to help out other developers? Visit our discussion forums.

    Sponsored by:

    New Articles

  • Exceptions and Performance
    Almost every time exceptions are mentioned in mailing lists and newsgroups, people say they're really expensive.Let's examine that claim, shall we?

  • Creating multilingual websites - Part 1
    Extend the existing globalization capabilities of .NET to create flexible and powerful multilingual web sites. First, create a custom ResourceManager, and then create custom localized-capable server controls to easily deploy multilingual functionality.

  • Parameter passing in C#
    Many people have become fairly confused about how parameters are passed in C#, particularly with regard to reference types. This page should help to clear up some of that confusion

  • Most Popular Articles

  • LDAP, IIS and WinNT Directory Services
    This article explains how to use .NET Directory Services to retrieve and search directory objects, create new directory objects and edit or delete existing directory objects. Describes Active Directory Application Mode (ADAM) and how to use the IIS, WinNT and LDAP directory (ADSI) provider.

  • An in-depth look at WMI and instrumentation, Part II
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article is the second part of a two-part series.

  • An in-depth look at WMI and instrumentation, Part I
    WMI stands for Windows Management Instrumentation and, as the name indicates, is about managing your IT infrastructure this article provides an in-depth look at WMI and MOM 2005

  • New Books

  • Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
    What’s new and how to use it! That’s what this book delivers if you’re a VB developer who’s interested in upgrading from ASP.NET 1.x to ASP.NET 2.0.

  • C# in easy steps
    Learn to program with Microsoft’s premier programming language. No previous programming knowledge is assumed. With numerous easy-to-follow examples, this title explains the essentials of object-oriented programming with C#.

  • Murach's ASP.NET web programming with VB.NET
    Murach's ASP.NET web programming with VB.NET by Doug Lowe and Anne Prince is a in depth training and reference book for ASP.NET programming using VB.NET. The book builds upon Murach's previous books and covers more advanced concepts for programming ASP.NET pages.

  • Got Code?

    if you have any article , source code , or anything else you'd like to share with this community that you think others might find useful, please submit it here and we will gladly make it available on this site. submit@developerland.com.
    Partners

    All articles are copyrighted by their individual authors unless otherwise specified , everything else Copyright ©2004-2006 DeveloperLand