将Linq DataContext连接到相同但名称不同的数据库 [英] Connecting Linq DataContext to Identical but differently named databases

查看:61
本文介绍了将Linq DataContext连接到相同但名称不同的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为不同的客户使用多个数据库,他们拥有相同的表格。我创建了一个datacontext来使用这些表,所有这些都与我创建datacontext的数据库完美配合。我遇到的问题是在运行时更改连接字符串和数据库名称的名称,这取决于谁登录,以便他们看到正确的数据。以下是生成的Ent.designer.vb文件中的前几行。

I am using several databases for different customers, they hold identical tables. I made a datacontext to use the tables and all works fine with the database that I created the datacontext from. The problem I'm having is changing the name of the connectionstring and database name at runtime so depending upon who's logged in so they see the correct data. Below are the first few lines from the generated Ent.designer.vb file.

< System .Data.Linq.Mapping.DatabaseAttribute(Name:= " ST" )> _

<System.Data.Linq.Mapping.DatabaseAttribute(Name:="ST")> _

部分 Public Class EntDataContext

Partial Public Class EntDataContext

继承 System.Data.Linq.DataContext

Inherits System.Data.Linq.DataContext

私人 共享 mappingSource As System.Data.Linq.Mapping.MappingSource = AttributeMappingSource

Private Shared mappingSource As System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource

 

MyBase .New( 全局 .System.Configuration.ConfigurationManager.ConnectionStrings( " STconnectionstring1" )。ConnectionString,mappingSource)

MyBase.New(Global.System.Configuration.ConfigurationManager.ConnectionStrings("STconnectionstring1").ConnectionString, mappingSource)

OnCreated()

OnCreated()

End Sub

End Sub

数据库名称为ST,连接字符串为STconnectionstring1。我想要做的就是以编程方式更改这两个值,以便不同的连接字符串从不同的数据库填充datacontext。我已经坚持了大约一个星期,认为这很简单,但似乎不可能做到,尝试了我能想到的一切都没有成功。是否有可能做到这一点?感谢任何已经完成或知道如何工作的人。

干杯,

Simon

推荐答案

寻找其他构造函数。应该有一个接受连接字符串。

Look for other constructors.  There should be one that accepts a connection string.

这是它在C#中的样子




Code Snippet


这篇关于将Linq DataContext连接到相同但名称不同的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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