用linq到sqlserver动态连接到sql server [英] connecting to sql server dynamically with linq to sqlserver

查看:190
本文介绍了用linq到sqlserver动态连接到sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我正在编写一个需要在运行时连接到sql server(带有预定义表)的项目。例如,我想从用户那里获取sqlserver的IP,端口,用户名和密码,然后连接到它并获取一些数据。我的数据库表结构对所有用户都相同,但IP,端口,用户名和密码不同。那么如何在运行时动态连接?



更多信息:

有些公司他们有一个指定的预定义数据库。我的程序将安装在公司客户端计算机上。所以,我需要获取数据库服务器的IP,端口,用户名和密码,然后创建连接字符串并使用自己的连接字符串连接到每个数据库。





谢谢

Hello everyone
I am writing a project which needs to connect to sql server ( with predefined tables) in run time. for example I want to get IP, port, username and password of sqlserver from user and then connect to it and fetch some data. my database table structure is same for all users but IP, port, username and password is different. so how can I connect dynamically in run time?

more information:
there are some Companies which they have a specified predefined database. my program will be installed on the companies client computers. so, I need to get IP, port, username and password of database servers and then create connection string and connect to each database with its own connection string.


thanks

推荐答案

连接对象有一个构造函数,它接受一个连接字符串,所以只需要传递它,它就不会使用配置文件中有一个。
There is a constructor for the connection object which takes a connection string, so just pass it through and it won't use the one in your config file.


您可以在后端配置连接字符串。几乎所有的DB dll都将connectionstring作为属性。



http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v = vs.110)。 aspx [ ^ ]



您可以参考以下链接动态创建连接字符串



http:// stackoverflow。 com / questions / 16511244 / how-to-create-connection-string-dynamic-in-c-sharp [ ^ ]



如果你认为那个e数据库对于所有客户端都是相同的,那么你只能导入所需的DLL,例如:Sql DB只需要System.Data.SqlClient,否则你可以添加所需的dll,并根据客户端使用的数据库启动连接字符串



所需DLL的参考: http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp [ ^ ]
You can configure the connection string at the backend. Almost all the DB dll have connectionstring as property.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx[^]

You can refer the following links to create the connection string dynamically

http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp[^]

If you think that the database would be same for all the clients then you can import only the required DLL eg: Sql DB needs only System.Data.SqlClient else you can add the needed dll and based on the DB that the client uses you can initiate the connection string

Reference for the DLL's required: http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp[^]


这篇关于用linq到sqlserver动态连接到sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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