如何使用ASP.Net C#Application处理多个SqlServer 2005数据库 [英] How to work with Multiple SqlServer 2005 databases with ASP.Net C# Application

查看:59
本文介绍了如何使用ASP.Net C#Application处理多个SqlServer 2005数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



正在创建一个asp.net Web应用程序。在这里我想使用2个Sqlserver数据库。



我在Sqlserver2005中有两个数据库作为StudentDB和CollegeDB。现在我想在我的程序中使用这两个数据库。



所以我应该如何使用,以及如何在web.configfile中存储这两个数据库。



请帮助。



我想在一个Asp.Net应用程序中使用2个数据库。



请帮忙谢谢。

解决方案

如前所述,您可以存储两个不同的连接字符串然后可以打开两个单独的SqlConnection对象,每个数据库。



但是,为了给您提供替代方案,您还可以在任一Sql Server实例中定义链接服务器。使用链接服务器可以实现只连接到一个数据库服务器然后再连接到另一个数据库服务器的情况。这样你就需要在你的应用程序中只有一个连接。



要查询链接服务器后面的数据,你需要使用特殊的四部分表格的表示法。如果这感觉很麻烦,你总是可以定义一个视图来隐藏表的位置。



关于链接服务器的更多信息:已链接的服务器 [ ^ ]


在Web.config文件中添加两个ConnectionString,其名称不同,如下所示:



< connectionstrings> 
< add name = ConnectionString1 connectionstring = 初始目录= dbName1;数据源= ServerName;集成安全性= SSPI; providername = System.Data.SqlClient />
< add name = ConnectionString2 connectionstring = 数据源= dbName2;初始目录= ServerName;集成安全性=真 >
providerName = System.Data.SqlClient />
< / add > < / connectionstrings >


Hi Friends,

am creating a asp.net Web application. In this i want to use 2 Sqlserver databases.

I have two databases in Sqlserver2005 as StudentDB and CollegeDB. NOW i want to use these 2 databases in my program.

so how should i use, and how to store these two databases in web.configfile.

Please help.

I want to use 2 databases with one Asp.Net Application.

Please Help THANKS.

解决方案

As already suggested you can store two different connection strings and then you can open two separate SqlConnection objects, on per each database.

However, to give you an alternative, you can also define a linked server in either of the Sql Server instances. Using linked server you can achieve a situation where you connect only to one database server which then again connects to another. This way you would need to have only one connection in your application.

To query the data ''behind'' the linked server you need to use special four-part notation for tables. If this feels cumbersome you can always define a view to hide the location of a table.

More about linked servers: Linked Servers[^]


Add Two ConnectionString in your Web.config file with different name''s like this:

<connectionstrings>
    <add name="ConnectionString1" connectionstring="Initial Catalog=dbName1;Data Source=ServerName;Integrated Security=SSPI;" providername="System.Data.SqlClient" />
    <add name="ConnectionString2" connectionstring="Data Source=dbName2;Initial Catalog=ServerName;Integrated Security=True">
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>


这篇关于如何使用ASP.Net C#Application处理多个SqlServer 2005数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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