[英]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.