【问题标题】:SQL Express connection string: mdf file location relative to application locationSQL Express 连接字符串:mdf 文件位置相对于应用程序位置
【发布时间】:2011-03-30 20:57:16
【问题描述】:

我正在使用 SQL Express 数据库作为 c# 中单元测试项目的一部分。我的数据库位于此处:

./Databases/MyUnitTestDB.mdf

我想在app.config 中使用相对路径或变量,而不是将我的连接字符串定义为:

AttachDbFilename=C:\blah\blah\blah\yea\yea\yea\MyApplication\Databases\MyUnitTestDB.mdf 

我见过|DataDirectory| 的用法,但我认为这仅适用于网络应用程序是否正确?

我想在应用程序配置文件中控制这一点,因为在生产中应用程序使用托管的 sql 数据库。

【问题讨论】:

    标签: c# sql connection-string database-connection sql-server-express


    【解决方案1】:

    谢谢大家,我综合了你们的回答。

    在我的 app.config 文件中,我的连接字符串定义如下

    <add name="MyConnectionString"
        connectionString="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Database=MyDatabaseForTesting;Trusted_Connection=Yes;" />
    

    在我的单元测试类中,我使用以下设置 DataDirectory 属性

    [TestInitialize]
    public void TestInitialize()
    {
        AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Databases"));
    
        // rest of initialize implementation ...
    }
    

    【讨论】:

    【解决方案2】:

    是的,|DataDirectory| Web 应用程序选择 Web 应用程序的 App_Data 目录。

    在非 Web 应用程序中,根据 .NET Framework,可以使用它,也可以使用 AppDomain.SetData

    对其进行更改

    但是您还有另外两种创建连接的可能性:

    1.- 使用相对路径:

    String con ="... AttachDbFilename=Databases\MyUnitTestDB.mdf ... ";
    

    2.- 获取应用路径并添加到String.
    在 c# Windows 应用程序中,您可以使用 Application.StartupPath

     String con= " ... AttachDbFilename=" + Application.StartupPath + "\Databases\MyUnitTestDB.mdf ... ";
    

    根据应用程序类型或启动模式,您会获得不同的属性。例如:

    • Application.StartupPath -- 启动应用程序的exe应用程序的启动路径
    • Application.ExecutablePath -- 开始路径 exe 应用程序的名称,用于统计应用程序 但是要使用 Application,您需要将 System.Windows.Forms 包含在控制台应用程序中,例如控制台应用程序。

    • System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) -- 从当前程序集中获取路径 "dll,exe,..." 不受应用程序类型、路径更改的影响。 .. 总是在 Assemby 驻留时返回目录。

    • Environment.CurrentDirectory -- 当前目录。例如,如果您导航到文件夹,则可以更改此设置。

    您可以在此处找到有关不同连接字符串选项的更多信息 http://www.connectionstrings.com/sql-server-2005

    【讨论】:

    • 感谢 Dubas,选项 1 是我想要使用的,但我无法使其正常工作。
    • 选项 1 取决于您的执行路径。如果您的 UInit 测试使用与您的应用程序不同的路径执行,则使用相对路径可能会为您提供与预期路径不同的路径。
    • @Dubas |数据目录|变量不仅适用于 Web 应用程序,您可以在任何地方使用它
    【解决方案3】:

    我花了一整天的谷歌搜索来解决这个问题,终于从this得到了一个线索

    这是我的解决方案:
    1. 使用 |数据目录|在连接字符串中

    <add name="NorthwindConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDbFilename=|DataDirectory|\Northwind.mdf;User Instance=True" providerName="System.Data.SqlClient" />
    

    2.在ClassInitialize中设置DataDirectory

    [ClassInitialize()]
    public static void MyClassInitialize(TestContext testContext)
    {
        string baseDir = AppDomain.CurrentDomain.BaseDirectory;
        int index = baseDir.IndexOf("TestResults");
        string dataDir = baseDir.Substring(0, index) + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
    }
    

    【讨论】:

      【解决方案4】:

      我正在使用 VS2010 和 C#3.0 构建一个简单的 Windows 窗体应用程序。也使用 SQL Express 2008 RC2。

      我可以在连接字符串中单独使用:|DataDirectory|MyDb.mdf,而无需更改任何其他内容。 |DataDirectory| 指向我的 .exe 文件的位置。

      我认为这将是你们所有人都会尝试的第一件事,所以这就是我指定我的 VS 和 SQL 版本的原因。或者它可能是 C#3.0 的新手。

      我的完整连接字符串:

      "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|App_Data\ThumbsUpPlayer.mdf;Database=ThumbsUpPlayer;Trusted_Connection=Yes;"
      

      请注意,我在我的应用程序中添加了一个“App_Data”文件夹,因为我习惯了该文件夹中的 Db,VS 无法识别该文件夹。

      【讨论】:

      • 完美解决方案。谢谢。
      【解决方案5】:

      我这里没有 Visual Studio,但是呢:

      using System.IO;
      using System.Windows.Forms;
      
      string appPath = Path.GetDirectoryName(Application.ExecutablePath);
      AttachDBFilme = appPath + "\\MyUnitTestDB.mdf"
      

      【讨论】:

      • 我会建议一些类似的东西,也许与 ADO.NET 的 connection string builders 一起使用来组合最终的连接字符串。
      【解决方案6】:

      我做了以下事情。希望它可以帮助某人。

      AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + "../../App_Data"));
      

      【讨论】:

        【解决方案7】:

        SQL Server 连接中的动态路径

        SqlConnection  con="Server=.\SQLExpress;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Database=MyDatabaseForTesting;Trusted_Connection=Yes;" ;
        

        【讨论】:

          猜你喜欢
          • 2017-07-01
          • 2013-10-06
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-14
          相关资源
          最近更新 更多