更改ConnectionString

时间:2017-04-26 15:05:07

标签: c#

我有一个Win Form,其中包含一个名为Location_txtBx的文本框和一个运行OpenFileDialog的浏览按钮,该文件将所选文件传递给TextChanged事件上的文本框Location_txtBx,该文本将文本传递给名为Con的字符串。有没有办法更改connectionString的源部分,以便管理员可以更改数据库位置?

1 个答案:

答案 0 :(得分:0)

我现在添加了一个名为Acceptbtn的Button,并在on click事件中添加了代码。

if (File.Exists("D:\\Connect.file"))
{
    File.Delete("D:\\Connect.file");
}
string provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
string Security = ";Persist Security Info=False;";

string con = provider + Location_txtBx.Text + Security;
StreamWriter sw = new StreamWriter("D:\\Connect.file");
sw.WriteLine(con);
sw.Close();
Location_txtBx.Text = "";
Acceptbtn.Enabled = false;

我在每种形式中都使用了代码

string connect = System.IO.File.ReadAllText("D:\\Connect.file");
connection.ConnectionString = connect;

此工作并将连接字符串输出到重命名的文本文件Connect.file。但这似乎相当不安全。

相关问题