【问题标题】:find the sql connectionstring from a vb.net application从 vb.net 应用程序中查找 sql 连接字符串
【发布时间】:2011-04-22 18:00:33
【问题描述】:

我有 Visual Studio 2008,我正在做一个 ASP.NET 应用程序。 现在我想将我的页面链接到 SQL Server 2008。在我的页面 default.aspx 上,我放置了一个 gridview,然后我可以找到从那里到 sql server db 的连接字符串,然后我可以将其放入一个类中并拥有所有页面使用它。 这是找到网络上任何服务器的连接字符串的唯一方法还是有其他方法(可能是找到连接字符串的更好方法?)

任何帮助将不胜感激。

【问题讨论】:

    标签: sql vb.net connection-string


    【解决方案1】:

    不用gridview,你可以用2008年的WebConfigurationManager...

    这是来自 MSDN 的代码示例,它显示了这一点:

    <connectionStrings>
      <add 
        name="NorthwindConnectionString" 
        connectionString="Data Source=serverName;Initial 
        Catalog=Northwind;Persist Security Info=True;User 
        ID=userName;Password=password"
        providerName="System.Data.SqlClient"
      />
    </connectionStrings>
    

    这是使用此连接字符串的 VB.Net 代码。

    Dim rootWebConfig As System.Configuration.Configuration
        rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot")
    Dim connString As System.Configuration.ConnectionStringSettings
    If (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count) Then
        connString = rootWebConfig.ConnectionStrings.ConnectionStrings("NorthwindConnectionString")
        If Not (connString.ConnectionString = Nothing) Then
            Console.WriteLine("Northwind connection string = {0}", connString.ConnectionString)
        Else
            Console.WriteLine("No Northwind connection string")
        End If
    End If
    

    【讨论】:

    • 但是 web.config 如何知道我正在使用哪个数据库?连接字符串数据源只给出“服务器名称”。如果我更改该名称,用户名和密码呢?
    • 数据源为服务器和实例名称,初始目录为您的数据库名称。
    • 对不起。我正是这个意思。 id 和 password 在那里如何工作?我问是因为我必须在他们的数据库和计算机上处​​理某人的项目。如果我使用 gridview,我不需要问他们用户名和密码。使用这种方法,我需要用户名/密码还是在 web.config 中?
    • 您是在询问是否必须提示用户输入用户名和密码? gridview 可能只是在您的 web.config 中使用现有的连接字符串,因此它可以访问 WebConfigurationManager 使用的相同数据...我会查看 web.config 并查看您的连接字符串的外观喜欢...
    • 好的。所以 web.config 中的任何内容都是我应该使用的。如果是用户名和密码,网络配置管理器会处理这些。正确的?不提示输入用户名/密码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 2012-08-11
    • 2014-02-06
    相关资源
    最近更新 更多