|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to get the connectionstirng form a TableAdapterWe 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? Hi,
Each TableAdapter has an internal Connection property you might assing. -- Show quoteMiha Markic [MVP C#, INETA Country Leader for Slovenia] RightHand .NET consulting & development www.rthand.com Blog: http://cs.rthand.com/blogs/blog_with_righthand/ "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? > 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. -- Show quoteJohn "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? > > > "JT" <Jthayer@online.nospam> wrote in message No, it is internal.news:3AC93EEC-5C5A-4A94-AEB3-1648F689AF14@microsoft.com... > The TableAdapter's connection is a private property. -- Show quoteMiha 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? >> >> >> 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 -- Show quoteJohn "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? > >> > >> > >> > > > 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. -- Show quoteJohn "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? > > >> > > >> > > >> > > > > > > |
|||||||||||||||||||||||