Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Since to the previous question I myself answered, that I had discovered the solution, but that I did not know how I could solve it, I told myself that the best thing was to ask this question separately for give everyone a chance to answer.

So I summarize, I'm trying to find out, how to get the name of the variable added to the assembly to contain the connectiontring of a specific dataset. If I find that, then I greatly increase the performance of the .NET generic data access code.

What I have tried:

I search a whole day anything about it, without result. all i know is that i found someting by reading the .XSL of the Dataset... something useless when we are in code execution.
Posted
Updated 25-Apr-18 22:22pm

here may not be one.
It could be held in a configuration file - the best way - and read on demand when the connection needs to be established (or read into a local variable which only exists while a method is running)
It could be in a const value - and the compiler may optimise that to a in-line string.
It could be hard-coded into the app and not stored anywhere except the strings cache.

The best way to find out is to contact the authors and ask them - but they will likely respond "why do you need to do that?", I certainly would!
 
Share this answer
 
Comments
Frederic GIRARDIN 8-Mar-18 7:55am    
i make my own Dataset, i'm the Author : i just wish to deal with any Dataset, so i need to get the assembly variable name, to programmaly lock it (while instancing TableManagerAdapter and all Adapter), set it, use it and them unlock it.
Frederic GIRARDIN 26-Apr-18 4:31am    
a const value could be a solution, but only if we have an extension method to get and set it.
I have to wrote a protected function extension on dataset in my program.

It should take consideration of "_" character in dataset.getttype.name to split information. Then i should have enough information to compute my own connnectionString variable Name :

dataset.gettype.name = {ExtentionFormat_dataset_versionnumber} (example : ESTX_DataSet_1)
so ESTX is the dataset.gettype.name.split("_")(0) value and 1 is the dataset.gettype.name.split("_")(2) value.

According to my own work, Connectionstring Variable Name should be ESTX_ConnectionString_1.

Then this is easy to create some dataset sub extention, like Set_ConnectionString and Get_ConnectionString, based on GetConnectionStringName.

VB
Imports System.Runtime.CompilerServices

Module DataSetExtentions

<Extension()>
Protected function GetConnectionStringName(MyDataset as dataset) as string
   dim _szextention as string = MyDataSet.GetType.Name.Split("_")(0)
   dim _szVersion as string = MyDataSet.GetType.Name.Split("_")(2)
   Return _szextention & "_ConnectionString_" & _szVersion
end function

<Extension()>
Protected function Get_ConnectionString(MyDataSet as dataSet) as string
   return My.Settings(MyDataSet.GetConnectionStringName) 
end function

<Extension()>
Protected sub Set_ConnectionString(myDataSet as dataset, Myvalue as string)
   My.Settings(MyDataSet.GetConnectionStringName) = myValue
end sub
end module


So using the code should be :

VB
imports System.Reflection
imports System.oledb

public Class DataLayer

Private ds as dataset
Private _connectionString as string
Private _connection as oledbConnection
Private _TableManager As Object

Public sub New(File as string)
   'read file extention to set the Dataset Type
   dim _szExtension as string = system.io.file.getExtention(File)
   'set the connection string for the whole use
   _connectionstring = "Provider=...;DataSource=" & File & ";" & ...
   'set the connection
   _connection = new oledbconnection(_connectionstring)

   'now a pre process to open a minimalist dataset with only one intengible table, to read format version
SyncLock my.setting(ds.GetConnectionStringName)
   ds = new intangible_dataset(_conn)
   ds.Set_ConnectionString = _connectionstring
   'load TableAdapterManager
   _classname = System.Reflection.Assembly.GetExecutingAssembly().GetName.Name _
                .Replace(" ", "_") & "." & DataSet.GetType.Name & _
               "TableAdapters.TableAdapterManager"
    _TableManager = System.Reflection.Assembly.GetExecutingAssembly.CreateInstance(_classname)
   InitTableManager()

   'open connection
   _connection.open
   'read Version format
   dim dtConfig as datatable = loadTable("CONFIG")
   dim szVersion as string = dtConfig.Rows(0)("Version")
   'close connection
   _connection.close
   ds.Dispose
   ds = nothing
   CType(_TableManager, IDisposable).Dispose()
   _TableManager = nothing
   'create a variable to store dataset type 
   dim tDs as system.type
   'compute type name of Dataset
   dim _className as string ' = ...
   'create dynamic instance of tDS
   ds = System.Reflection.Assembly.GetExecutingAssembly.CreateInstance(_classname)
   ds.Set_ConnectionString = _connectionstring
   'Dynamic load of _tableAdapterManager
   _classname = System.Reflection.Assembly.GetExecutingAssembly().GetName.Name _
                .Replace(" ", "_") & "." & DataSet.GetType.Name & _
               "TableAdapters.TableAdapterManager"
    _TableManager = System.Reflection.Assembly.GetExecutingAssembly.CreateInstance(_classname)
   InitTableManager()
End SyncLock 
   'ready to use : open, close, loadtable
end sub

Private Sub InitTableManager()
  'give the connection
  _TableManager.Connection = _Connection

  'set every tableAdapter for each property
  For Each _table As DataTable In _Dataset.Tables
     Dim _Adapter As Object = Nothing
     Try
        Dim _classname As String = Assembly.GetExecutingAssembly() _
                                 .GetName.Name.Replace(" ", "_") & "." &
                                 _Dataset.GetType.Name & "TableAdapters." & 
                                 _table.TableName & "TableAdapter"
        _Adapter = Assembly.GetExecutingAssembly.CreateInstance(_classname)
        CallByName(_TableManager, _table.TableName & "TableAdapter", CallType.Set, _Adapter)
'this part is no more needed because we are able to set the connectionstring used by the the current dataset
#if false then
        Dim pinfo As PropertyInfo = 
        _Adapter.GetType.GetProperties(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance).Where(Function(w) w.Name = "Connection").FirstOrDefault

                If pinfo IsNot Nothing Then
                    pinfo.SetValue(_Adapter, _TableManager.Connection)
                Else
                    Stop
                End If
#End if
            Catch ex As Exception
                Debug.Print(ex.Message)
                stop
            End Try
        Next
    End Sub

Public function LoadTable(aTableName as string, optional clone as boolean = false) as datatable
 dim _Adapter as object
 'if connection is close then open it
 if _connection.state = close then _connection.Open()
 'dynamically tableAdapterproperty for _tableAdapter Classname
 dim _propertyName as string = aTableName & "TableAdapter"
 'get adapter from TableAdapterManager
 _Adapter = CallByName(_TableManager, _propertyName, CallType.Get)
 Dim dt As DataTable = Nothing
 If clone Then
    dt = _ds.Tables(aTableName).Clone
 Else
    dt = _ds.Tables(aTableName)
 End If

 SyncLock dt
   _Adapter.Fill(dt)
 End SyncLock

 return dt
end function

Public readonly DataSet as Dataset
   get
      return _ds
   end get
end property
End class
 
Share this answer
 
v2
Comments
Frederic GIRARDIN 26-Apr-18 4:35am    
note that : all we need is to keep the connectionstring value changed during _TableAdapterManager instanciation. Then ConnectionString is set, for the whole use of DataLayer Class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900