How to get the connectionstirng form a TableAdapter

A

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?
 
M

Miha Markic [MVP C#]

Hi,

Each TableAdapter has an internal Connection property you might assing.
 
G

Guest

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.
 
G

Guest

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 said:
JT said:
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
 
G

Guest

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


JT said:
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 said:
JT said:
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


:

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top