如何找到作为数据集连接字符串的程序集变量的名称? [英] How to find the name of the assembly variable serving as connectionstring to a dataset?

查看:154
本文介绍了如何找到作为数据集连接字符串的程序集变量的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从上一个问题我自己回答,我发现了解决方案,但我不知道如何解决它,我告诉自己,最好的办法是分别提出这个问题,让每个人都有机会回答。



所以我总结一下,我试图找出,如何获取添加到程序集中的变量的名称以包含特定数据集的连接字符串。如果我找到了,那么我会大大提高.NET通用数据访问代码的性能。



我尝试过的方法:



我整整一天搜索一下,没有结果。我所知道的是,我通过阅读数据集的.XSL找到了一些东西...当我们处理代码时没什么用。

解决方案

这里可能不是一个。

它可以保存在配置文件中 - 最好的方式 - 并在需要建立连接时读取(或读入仅在方法运行时存在的局部变量)

它可以在 const 值中 - 并且编译器可以将其优化为内嵌字符串。

它可以硬编码到应用程序中,除了字符串缓存外没有存储。



最好的方法是联系作者并询问他们 - 但他们我可能会回答为什么你需要这样做?,我当然会这样做!


我必须在程序中的数据集上写一个受保护的函数扩展。



应该考虑dataset.getttype.name中的_字符来分割信息。然后我应该有足够的信息来计算我自己的connnectionString变量名称:



dataset.gettype.name = {ExtentionFormat_dataset_versionnumber}(例如:ESTX_DataSet_1)

所以ESTX是dataset.gettype.name.split(_)(0)值,1是dataset.gettype.name.split(_)(2)值。



根据我自己的工作,Connectionstring变量名称应为ESTX_ConnectionString_1。



然后这很容易创建一些数据集子扩展,如Set_ConnectionString和Get_ConnectionString,基于GetConnectionStringName。



  Imports  System.Runtime .CompilerServices 

模块 DataSetExtentions

< Extension()>
受保护的 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
返回 _szextention& _ ConnectionString _& _szVersion
end function

< Extension()>
受保护的 函数 Get_ConnectionString(MyDataSet as dataSet) as string
return My.Settings(MyDataSet.GetConnectionStringName)
end function

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





所以使用代码应该是:



  imports  System.Reflection 
imports System.oledb

public Class DataLayer

私有 ds as 数据集
Private _connectionString as string
私有 _connection as oledbConnection
私有 _TableManager 作为 对象

公共 sub (文件 as string
' 读取文件扩展名以设置数据集类型
dim _szExtension as string = system.io.file.getExtention(File)
' 设置整个使用的连接字符串
_connectionstring = Provider = ...; DataSource =&文件& ;& ...
' 设置连接
_connection = new oledbconnection(_connectionstring)

' 现在预售只打开一个简单表格的简约数据集的过程,读取格式版本
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版本格式
< span class =code-keyword> dim
dtConfig as datatable = loadTable( < span class =code-string> CONFIG)
dim szVersion as string = dtConfig.Rows( 0 )( 版本
' close连接
_connection.close
ds.Dispose
ds = 没有
CType (_ TableManager,IDisposable).Dispose()
_TableManager = nothing
' 创建存储数据集类型的变量
dim tDs < span class =code-keyword> as system.type
' 计算类型名称数据集
dim _className as 字符串 ' = ...
' 创建tDS的动态实例
ds = System.Reflection.Assembly.GetExecutingAssembly.CreateInstance(_classname)
ds.Set_ConnectionString = _connectionstring
' _tableAdapterManager的动态负载
_classname = System.Reflection.Assembly.GetExecutingAssembly()。GetName.Name _
.Replace( _)& & DataSet。 GetType .Name& _
TableAdapters.TableAdapterManager
_TableManager = System.Reflection.Assembly。 GetExecutingAssembly.CreateInstance(_classname)
InitTableManager()
结束 SyncLock
' 准备使用:打开,关闭,可装载
结束 sub

私有 Sub InitTableManager()
' 提供连接
_TableManager.Connection = _Connection

' 为每个属性设置每个tableAdapter
对于 每个 _table 作为 DataTable _Dataset.Tables
Dim _Adapter 作为 对象 =
尝试
Dim _classname As 字符串 = Assembly.GetExecutingAssembly()_
.GetName.Name.Replace( _) &安培; &
_Dataset。 GetType .Name& TableAdapters。&
_table.TableName& TableAdapter
_Adapter = Assembly.GetExecutingAssembly.CreateInstance(_classname)
CallByName (_TableManager,_table.TableName& TableAdapter,CallType。设置,_Adapter)
' 此部分不再需要,因为我们能够设置当前数据集使用的连接字符串
#if false then
Dim pinfo 作为 PropertyInfo =
_Adapter。 GetType .GetProperties(BindingFlags。公共 BindingFlags.NonPublic BindingFlags.Instance) .Where(函数(w)w.Name = 连接)。FirstOrDefault

如果 pinfo IsNot Nothing 然后
pinfo.SetValue(_Adapter,_TableManager.Connection)
其他
停止
结束 如果
#End if if
Catch ex As 异常
Debug.Print(ex.Message)
stop
结束 尝试
下一步
结束 Sub

公共 功能 LoadTable(aTableName as string 可选 clone as boolean = false as datatable
dim _Adapter as object
' 如果连接已关闭然后将其打开
if _connection.state = close 然后 _connection.Open()
' 动态tableAdapter属性为_tableAda pter Classname
dim _propertyName as string = aTableName& TableAdapter
' 从TableAdapterManager获取适配器
_Adapter = CallByName(_TableManager,_propertyName,CallType。获取
Dim dt As DataTable = Nothing
如果 clone 那么
dt = _ds.Tables(aTableName).Clone
其他
dt = _ds.Tables(aTableName)
结束 < span class =code-keyword>如果

SyncLock dt
_Adapter.Fill(dt)
结束 SyncLock

返回 dt
结束 function

公开 readonly DataSet as 数据集
获取
返回 _ds
结束 获取
结束 属性
结束


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.

解决方案

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!


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.

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 :

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


这篇关于如何找到作为数据集连接字符串的程序集变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆