如何在用户输入的app.config中编写连接字符串 [英] How to write connection string in app.config from user input

查看:76
本文介绍了如何在用户输入的app.config中编写连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生





i有一个app.config文件,我希望用户提供数据库名称等,并使用以下代码但它没有保存在配置文件中。



  var  miconex =  new  SqlConnectionStringBuilder(); 
miconex.DataSource = SQL-PC; miconex.InitialCatalog = control;
miconex.UserID = sa; miconex.Password = xxxx;

string saleconfritas = miconex.ToString();
MessageBox.Show(saleconfritas);
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string modif = string .Format(ConfigurationManager.ConnectionStrings [ con]。ConnectionString,saleconfritas);
config.Save(ConfigurationSaveMode.Minimal);





我的app.config文件是



< pre lang =xml> < configuration >
< connectionstrings > ;
< add 名称 = con providername = System.Data.sqlclient connectionstring = < br mode = hold = /> / >
< / connectionstrings >
< / configuration >





当我运行代码并检查它仍然显示相同时。

解决方案

 <   configuration  >  
< connectionStrings < span class =code-keyword>>
< add 名称 = 测试 connectionString = 数据源= .;初始目录= MyDBTest; IntegratedSecurity = True providerName = < span class =code-keyword> System.Data.SqlClient / >
< / connectionStrings >
< / configuration >







< br $> b $ b

  var  connection = 
System.Configuration.ConfigurationManager.ConnectionStrings [ 测试];


本文将帮助您实现目标



在运行时为C#Windows应用程序配置App.Config文件中的连接字符串 [ ^

dear sir


i have an app.config file and i want the user give the database name etc and use the following code but it does not save in config file.

var miconex = new SqlConnectionStringBuilder();
     miconex.DataSource = "SQL-PC"; miconex.InitialCatalog = "control";
     miconex.UserID = "sa"; miconex.Password = "xxxx";

     string saleconfritas = miconex.ToString();
     MessageBox.Show(saleconfritas);
         System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
         string modif = string.Format(ConfigurationManager.ConnectionStrings["con"].ConnectionString,saleconfritas) ;
         config.Save(ConfigurationSaveMode.Minimal);



my app.config file is

<configuration>
	<connectionstrings>
		<add name="con" providername="System.Data.sqlclient" connectionstring="<br mode=" hold=" />			 " />
	</connectionstrings>
</configuration>



When i run the code and check it still shows same as it is.

解决方案

<configuration>
    <connectionStrings>
        <add name="Test" connectionString="Data Source=.;Initial Catalog=MyDBTest;IntegratedSecurity=True" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>






var connection =
    System.Configuration.ConfigurationManager.ConnectionStrings["Test"];


This article would be helpful you to achieve your goal

Configuring Connection String in App.Config File During Runtime For a C# Windows Application[^]


这篇关于如何在用户输入的app.config中编写连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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