如何在连接字符串中使用变量?
How can I use variable in connection string?
我在连接字符串的变量方面遇到了问题。在我的 app.config 数据库路径中似乎没问题,但在形式上,我收到该连接字符串的错误。当我尝试添加时:
_connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"|DataDirectory|\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
数据库将无法正常工作;它无法在退出时保存数据。然而这很好用:
connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"C:\Users\Can\Desktop\c_sharp_gazi_installer" +
"\Gazi Installer\gazi_installer\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
我该如何解决这个问题?
编辑:我希望这会起作用
string DataDirectory = "";
string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
AppDomain.CurrentDomain.SetData("DataDirectory", folder);
_connectionString = _connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"" + DataDirectory + "\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
但它仍然给我 sqlclient sqlexception 错误。
EDIT2: 此错误表示连接已打开。我也不明白为什么这不起作用:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "(LocalDB)\MSSQLLocalDB";
builder["AttachDbFilename"] = @"C:\Users\Can\Desktop\c_sharp_gazi_installer\Gazi Installer\gazi_installer\gazi_db.mdf";
builder["Integrated Security"] = true;
builder["Connect Timeout"] = 30;
请有人帮忙。我要疯了。
.NET 有一个 connection string builder class。使用它,您可以将每个值(即数据源、集成安全性)分别设置为变量,然后使用 .ConnectionString
属性 获取连接字符串。
首先,你可以使用@"symbol"来避免使用这么多的转义字符。
那么,你在哪里定义了"DataDirectory"变量?
我在连接字符串的变量方面遇到了问题。在我的 app.config 数据库路径中似乎没问题,但在形式上,我收到该连接字符串的错误。当我尝试添加时:
_connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"|DataDirectory|\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
数据库将无法正常工作;它无法在退出时保存数据。然而这很好用:
connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"C:\Users\Can\Desktop\c_sharp_gazi_installer" +
"\Gazi Installer\gazi_installer\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
我该如何解决这个问题?
编辑:我希望这会起作用
string DataDirectory = "";
string folder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().CodeBase);
AppDomain.CurrentDomain.SetData("DataDirectory", folder);
_connectionString = _connectionString = "Data Source = (LocalDB)\MSSQLLocalDB; " +
"AttachDbFilename = \"" + DataDirectory + "\gazi_db.mdf\"; " +
"Integrated Security = True; Connect Timeout = 30";
但它仍然给我 sqlclient sqlexception 错误。
EDIT2: 此错误表示连接已打开。我也不明白为什么这不起作用:
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder["Data Source"] = "(LocalDB)\MSSQLLocalDB";
builder["AttachDbFilename"] = @"C:\Users\Can\Desktop\c_sharp_gazi_installer\Gazi Installer\gazi_installer\gazi_db.mdf";
builder["Integrated Security"] = true;
builder["Connect Timeout"] = 30;
请有人帮忙。我要疯了。
.NET 有一个 connection string builder class。使用它,您可以将每个值(即数据源、集成安全性)分别设置为变量,然后使用 .ConnectionString
属性 获取连接字符串。
首先,你可以使用@"symbol"来避免使用这么多的转义字符。 那么,你在哪里定义了"DataDirectory"变量?