【问题标题】:Setting DataDirectory to location of executable将 DataDirectory 设置为可执行文件的位置
【发布时间】:2012-11-16 01:44:30
【问题描述】:

我正在开发一个使用 SQL Server CE 数据库的应用程序。对于我的产品的最终最终版本,我将把我的数据库放在一个特定的位置,但是,出于测试目的,我需要把它放在与程序的可执行文件相同的位置。问题是我的 DataDirectory 指向了最终版本中它所在的目录。有没有办法找出可执行文件的文件位置并将 DataDirectory 设置为该位置?

更新:

我尝试了下面的代码,但现在它甚至不会将数据加载到 DataGridView 中。

string folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
AppDomain.CurrentDomain.SetData("DataDirectory", folder);

【问题讨论】:

    标签: c# sql-server-ce executable datadirectory


    【解决方案1】:
    string folder= System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
    string dbPath = System.IO.Path.Combine(folder, "DbName.sdf");
    string connString = "DataSource=" + dbPath;
    

    然后您可以使用connString 打开与数据库的连接。

    【讨论】:

    • 第一行有问题。请参阅上面我更新的问题。
    • 那你有什么连接字符串?是不是像|DataDirectory|\DbName.sdf
    • 我的 DataGridView 没有使用连接字符串,而是使用了数据集。
    • 你可以试试string folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
    • 这给了我同样的结果。它仍然不会从数据库中获取任何数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2018-08-11
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    相关资源
    最近更新 更多