动态更改asp.net中的SqlDataSource连接字符串 [英] change SqlDataSource connection string in asp.net dynamically

查看:90
本文介绍了动态更改asp.net中的SqlDataSource连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net(网络表单)中有一个页面,到现在为止只有一个数据库.

I have a page in asp.net (web forms) that up until now only had one database for data.

因此,所有需要数据库中某些内容的控件都使用数据源和相同的连接字符串.

So all the controls that require something from the database use a datasource and the same connection string.

<asp:SqlDataSource id="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:DefaultConnectionString %>" 
                    ProviderName="System.Data.SqlClient" 
                                        SelectCommand="SELECT ... ">
</asp:SqlDataSource>

到目前为止,由于我只有一个数据库,所以我只需要一个连接字符串,但是由于我希望某些用户连接到数据库A而其他用户连接到数据库B,这已经改变了

Up until now I only needed one connection string since I had only one database but this has changed since I want some users to connect to database A and others to database B

解决该问题的正确方法是什么?最好没有太多变化...

What would be the correct way to go about it? Preferably without many changes...

推荐答案

您可以在确定要使用哪一个(即

You can change it programatically after you determine which one you want to use, i.e.

SqlDataSource1.ConnectionString = "whateveryouwant";

不必在控件中内联设置.

It doesn't have to be set inline in the control.

这篇关于动态更改asp.net中的SqlDataSource连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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