从C#更新连接字符串永久 [英] updating connection string permanenlty from c#
问题描述
大家好,
我在使用c#编写/更新app.config文件中的连接字符串时遇到问题.我尝试了其他解决方案,但是没有解决方案可以将连接字符串永久保存在app.config中.所有解决方案都将连接字符串临时保存在app.config.exe中,该字符串在退出时会丢失数据.
任何帮助将不胜感激.
问候,
Shahzad
Hi every one,
I am facing problem in writing/ updating connection string in app.config file using c#. I tried different solutions but no solution could save connection string permanently in app.config. All solutions save connection string temporarily in app.config.exe which lost data when exit.
Any help will be highly appreciated.
Regards,
Shahzad
推荐答案
app.config
---------------
app.config
---------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="constr" connectionString="Data Source=Server_IP/Server_Name;Initial Catalog=Database_Name;User Id=Your_ID;Password=Your_Password;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
c#
-----
SqlConnection连接=新的SqlConnection();
connection.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings ["constr"]);
c#
-----
SqlConnection connection = new SqlConnection();
connection.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["constr"]);
这篇关于从C#更新连接字符串永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!