从C#更新连接字符串永久 [英] updating connection string permanenlty from c#

查看:85
本文介绍了从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆