简体   繁体   English

浏览SQL Server数据库的数据目录不起作用

[英]Browsing datadirectory for SQL Server database does not work

I have hardcoded the data directory for the database. 我已经硬编码了数据库的数据目录。 Since I want to avoid it, I have decided to use the FolderBrowserDialog and store the dialog string into the application settings. 由于我想避免这种情况,所以我决定使用FolderBrowserDialog并将对话框字符串存储到应用程序设置中。

This is the hardcoded code snippet, which can open the SQL connection: 这是硬编码的代码段,可以打开SQL连接:

AppDomain.CurrentDomain.SetData("DataDirectory", @"C:\Users\Osman\Documents\Visual Studio 2013\Projects\CompanyWPF\CompanyWPF\");

try
{
    using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Produkt.mdf;Integrated Security=True"))
    {
        con.Open();
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

But when I'm using the stored string from the application settings: 但是,当我从应用程序设置中使用存储的字符串时:

AppDomain.CurrentDomain.SetData("DataDirectory",@"" + Properties.Settings.Default.ConnectionString);

try
{
    using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=@|DataDirectory|\Produkt.mdf;Integrated Security=True") )
    {
        con.Open();
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

then the following error occurs: 然后会发生以下错误:

Invalid value for key 'attachdbfilename'. 密钥“ attachdbfilename”的值无效。

I have set a breakpoint, and the stored string from the application setting has the value: 我已经设置了一个断点,并且应用程序设置中存储的字符串具有以下值:

C:\\Users\\Osman\\Documents\\Visual Studio 2013\\Projects\\CompanyWPF\\CompanyWPF\\

I do not sure what you are going to do but i think here is the problem 我不确定您要做什么,但我认为这是问题所在

            using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=@|DataDirectory|\Produkt.mdf;Integrated Security=True") )

Use This Instead 用这个代替

using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Produkt.mdf;Integrated Security=True") )

Note : Remove @ From this "@|DataDirectory| " 注意:从此“ @ | DataDirectory |”中删除@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 使用 |DataDirectory| 后服务器数据库未更新在 C# Windows 表格项目中 - SQL Server database not updating after using |DataDirectory| in C# Windows Form project 使用asp.net和SQL Server浏览文件并将其上传到数据库 - Browsing and Uploading a file to database using asp.net and sql server 另一台计算机通过 |DataDirectory| 连接 SQL 数据库失败 - SQL database connection failure in another computer through |DataDirectory| 为什么使用 DataDirectory 不起作用但使用完整路径有效? - Why does using DataDirectory not work but using a full path works? 在安装exe应用程序中无法连接到本地SQL Server数据库 - Connecting to a local SQL Server database does not work in the setup exe application | DataDirectory目录| 用于EF更新数据库 - |DataDirectory| for EF Update-Database 是否使用c#对SQL Server数据库文件进行加密和解密? - Does Encrypt and Decrypt work for SQL Server database files using c#? C#WPF应用程序无法在使用SQL Server CE本地数据库的另一台PC上运行 - C# WPF app does not work on another PC using SQL Server CE local database SQL数据库连接无法正常工作 - SQL database Connection does not Work Properly 更改数据库的默认路径或更改 |DataDirectory| 定义 - Changing the default path of a database or changing the |DataDirectory| definition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM