【问题标题】:from one ASP page to other with variable and change connection string使用变量从一个 ASP 页面到另一个页面并更改连接字符串
【发布时间】:2010-11-22 12:22:48
【问题描述】:

我在第一页有 2 个 ASP 页面,用户选择数据库名称,在其他页面获取数据库名称并将连接字符串更改为其他数据库 如何通过单击按钮将变量从一个页面传递到另一个页面以及如何在其他页面中接收此变量以及如何更改连接字符串 连接字符串在 web.config 中是这样的:

<connectionStrings>
    <add name="Northwind" connectionString="Provider=sqloledb;Data Source=.;Initial Catalog=Northwind;User Id=sa;Password=sa; Connect Timeout=10"/>
</connectionStrings>

请尽快帮助我

【问题讨论】:

    标签: asp.net sql database


    【解决方案1】:

    不要让用户选择完整的连接字符串,这是一个巨大的安全问题,而且对用户自己来说也很麻烦。

    相反,在配置文件中有几个连接字符串:

    <connectionStrings>
        <add name="Northwind" connectionString="Provider=sqloledb;Data Source=.;Initial Catalog=Northwind;User Id=sa;Password=sa; Connect Timeout=10"/>
        <add name="Westwind" connectionString="..."/>
        <add name="Eastwind" connectionString="..."/>
    </connectionStrings>
    

    然后仅在 URL 上传递 name,例如MyPage.aspx?connstringname=Eastwind 最后在您读取连接字符串时使用 URL 中的该参数:

    string strConnString = ConfigurationManager.ConnectionStrings[Request.QueryString["connstringname"]].ConnectionString;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多