Home All Groups Group Topic Archive Search About

How to get the connectionstirng form a TableAdapter

Author
11 Jul 2006 1:26 AM
ad
We have to assign a connection string to a TableAdpater when we desgin a
TableAdapter in desgin time.
How can I get the connection string  in run time?

Author
11 Jul 2006 7:49 AM
Miha Markic [MVP C#]
Hi,

Each TableAdapter has an internal Connection property you might assing.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"ad" <fly***@wfes.tcc.edu.tw> wrote in message
news:enajHkIpGHA.504@TK2MSFTNGP05.phx.gbl...
> We have to assign a connection string to a TableAdpater when we desgin a
> TableAdapter in desgin time.
> How can I get the connection string  in run time?
>
Author
12 Jul 2006 5:13 AM
JT
The TableAdapter's connection is a private property.  So.. go to your
dataset.xsd, right click, hit View Code.  This will generate a partial class
that you can use to extend your TableAdapter.  For example, you could create
a method that takes a conn string as a parameter.  Since you are in the
TableAdapter's class, you have access to the connection, and can get or set
the conn string.
--
John


Show quote
"ad" wrote:

> We have to assign a connection string to a TableAdpater when we desgin a
> TableAdapter in desgin time.
> How can I get the connection string  in run time?
>
>
>
Author
12 Jul 2006 8:13 AM
Miha Markic [MVP C#]
"JT" <Jthayer@online.nospam> wrote in message
news:3AC93EEC-5C5A-4A94-AEB3-1648F689AF14@microsoft.com...
> The TableAdapter's connection is a private property.

No, it is internal.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

So.. go to your
Show quote
> dataset.xsd, right click, hit View Code.  This will generate a partial
> class
> that you can use to extend your TableAdapter.  For example, you could
> create
> a method that takes a conn string as a parameter.  Since you are in the
> TableAdapter's class, you have access to the connection, and can get or
> set
> the conn string.
> --
> John
>
>
> "ad" wrote:
>
>> We have to assign a connection string to a TableAdpater when we desgin a
>> TableAdapter in desgin time.
>> How can I get the connection string  in run time?
>>
>>
>>
Author
12 Jul 2006 5:51 PM
JT
Miha is correct.  Here is the code Visual Studio generates....
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "2.0.0.0"),  _
     System.ComponentModel.DesignerCategoryAttribute("code"),  _
     System.ComponentModel.ToolboxItem(true),  _
     System.ComponentModel.DataObjectAttribute(true),  _

System.ComponentModel.DesignerAttribute("Microsoft.VSDesigner.DataSource.Design.TableAdapterDesigner, Microsoft.VSDesigner"& _
        ", Version=8.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"),  _

System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")>  _
    Partial Public Class myTableAdapter
        Inherits System.ComponentModel.Component

        Private WithEvents _adapter As System.Data.SqlClient.SqlDataAdapter

        Private _connection As System.Data.SqlClient.SqlConnection

        Private _commandCollection() As System.Data.SqlClient.SqlCommand

        Private _clearBeforeFill As Boolean

<System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
        Friend Property Connection() As System.Data.SqlClient.SqlConnection
            Get
                If (Me._connection Is Nothing) Then
                    Me.InitConnection
                End If
                Return Me._connection
            End Get
            Set
                Me._connection = value
                If (Not (Me.Adapter.InsertCommand) Is Nothing) Then
                    Me.Adapter.InsertCommand.Connection = value
                End If
                If (Not (Me.Adapter.DeleteCommand) Is Nothing) Then
                    Me.Adapter.DeleteCommand.Connection = value
                End If
                If (Not (Me.Adapter.UpdateCommand) Is Nothing) Then
                    Me.Adapter.UpdateCommand.Connection = value
                End If
                Dim i As Integer = 0
                Do While (i < Me.CommandCollection.Length)
                    If (Not (Me.CommandCollection(i)) Is Nothing) Then

CType(Me.CommandCollection(i),System.Data.SqlClient.SqlCommand).Connection =
value
                    End If
                    i = (i + 1)
                Loop
            End Set
        End Property


To set the connection string, you can insert code such as

Namespace myDataSetTableAdapters

    Partial Public Class myDataTableTableAdapter

        Public WriteOnly Property AdapterConnectionString() As String
            Set(ByVal value As String)
                Me.Connection.ConnectionString = value
            End Set
        End Property

    End Class

End Namespace

--
John


Show quote
"Miha Markic [MVP C#]" wrote:

>
> "JT" <Jthayer@online.nospam> wrote in message
> news:3AC93EEC-5C5A-4A94-AEB3-1648F689AF14@microsoft.com...
> > The TableAdapter's connection is a private property.
>
> No, it is internal.
>
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & development www.rthand.com
> Blog: http://cs.rthand.com/blogs/blog_with_righthand/
>
>  So.. go to your
> > dataset.xsd, right click, hit View Code.  This will generate a partial
> > class
> > that you can use to extend your TableAdapter.  For example, you could
> > create
> > a method that takes a conn string as a parameter.  Since you are in the
> > TableAdapter's class, you have access to the connection, and can get or
> > set
> > the conn string.
> > --
> > John
> >
> >
> > "ad" wrote:
> >
> >> We have to assign a connection string to a TableAdpater when we desgin a
> >> TableAdapter in desgin time.
> >> How can I get the connection string  in run time?
> >>
> >>
> >>
>
>
>
Author
14 Jul 2006 1:15 PM
JT
Even easier,
You can set the ConnectionModifier to Public in the designer window for your
TableAdapter.  This will also change to public the same modifier for any
other TableAdapters in your dataset.  Set it here, and regenerating the code
will not erase your setting.  Do NOT set it in the Designer CODE file for the
dataset.
--
John


Show quote
"JT" wrote:

> Miha is correct.  Here is the code Visual Studio generates....
>
> <System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "2.0.0.0"),  _
>      System.ComponentModel.DesignerCategoryAttribute("code"),  _
>      System.ComponentModel.ToolboxItem(true),  _
>      System.ComponentModel.DataObjectAttribute(true),  _
>     
> System.ComponentModel.DesignerAttribute("Microsoft.VSDesigner.DataSource.Design.TableAdapterDesigner, Microsoft.VSDesigner"& _
>         ", Version=8.0.0.0, Culture=neutral,
> PublicKeyToken=b03f5f7f11d50a3a"),  _
>     
> System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")>  _
>     Partial Public Class myTableAdapter
>         Inherits System.ComponentModel.Component
>        
>         Private WithEvents _adapter As System.Data.SqlClient.SqlDataAdapter
>        
>         Private _connection As System.Data.SqlClient.SqlConnection
>        
>         Private _commandCollection() As System.Data.SqlClient.SqlCommand
>        
>         Private _clearBeforeFill As Boolean
>
>  <System.Diagnostics.DebuggerNonUserCodeAttribute()>  _
>         Friend Property Connection() As System.Data.SqlClient.SqlConnection
>             Get
>                 If (Me._connection Is Nothing) Then
>                     Me.InitConnection
>                 End If
>                 Return Me._connection
>             End Get
>             Set
>                 Me._connection = value
>                 If (Not (Me.Adapter.InsertCommand) Is Nothing) Then
>                     Me.Adapter.InsertCommand.Connection = value
>                 End If
>                 If (Not (Me.Adapter.DeleteCommand) Is Nothing) Then
>                     Me.Adapter.DeleteCommand.Connection = value
>                 End If
>                 If (Not (Me.Adapter.UpdateCommand) Is Nothing) Then
>                     Me.Adapter.UpdateCommand.Connection = value
>                 End If
>                 Dim i As Integer = 0
>                 Do While (i < Me.CommandCollection.Length)
>                     If (Not (Me.CommandCollection(i)) Is Nothing) Then
>                        
> CType(Me.CommandCollection(i),System.Data.SqlClient.SqlCommand).Connection =
> value
>                     End If
>                     i = (i + 1)
>                 Loop
>             End Set
>         End Property
>
>
> To set the connection string, you can insert code such as
>
> Namespace myDataSetTableAdapters
>
>     Partial Public Class myDataTableTableAdapter
>
>         Public WriteOnly Property AdapterConnectionString() As String
>             Set(ByVal value As String)
>                 Me.Connection.ConnectionString = value
>             End Set
>         End Property
>
>     End Class
>
> End Namespace
>
> --
> John
>
>
> "Miha Markic [MVP C#]" wrote:
>
> >
> > "JT" <Jthayer@online.nospam> wrote in message
> > news:3AC93EEC-5C5A-4A94-AEB3-1648F689AF14@microsoft.com...
> > > The TableAdapter's connection is a private property.
> >
> > No, it is internal.
> >
> > --
> > Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> > RightHand .NET consulting & development www.rthand.com
> > Blog: http://cs.rthand.com/blogs/blog_with_righthand/
> >
> >  So.. go to your
> > > dataset.xsd, right click, hit View Code.  This will generate a partial
> > > class
> > > that you can use to extend your TableAdapter.  For example, you could
> > > create
> > > a method that takes a conn string as a parameter.  Since you are in the
> > > TableAdapter's class, you have access to the connection, and can get or
> > > set
> > > the conn string.
> > > --
> > > John
> > >
> > >
> > > "ad" wrote:
> > >
> > >> We have to assign a connection string to a TableAdpater when we desgin a
> > >> TableAdapter in desgin time.
> > >> How can I get the connection string  in run time?
> > >>
> > >>
> > >>
> >
> >
> >

AddThis Social Bookmark Button