我们可以在“ app.config”文件中声明变量吗? [英] Can we declare variables in the 'app.config' file?

查看:146
本文介绍了我们可以在“ app.config”文件中声明变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要连接到SQL Server的表单,还有一个下拉列表,用于选择数据库列表并执行诸如主键检查等操作。

I have a form which needs to get connected to SQL Server, and I have a drop down for selecting the list of databases and perform operations like primary key checking, etc.

但是目前我的连接字符串如下:

But presently my connection string looks like this:

SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");

但是除了给定的数据库外,我还需要将其设为变量,以便可以将其连接到

But apart from the given database, I need to take it variable, so that I can connect it to the database I select from the dropdown.

我该怎么做?

推荐答案

嗯,您可以像这样声明变量

Hmm you can declare your variables like this

<appSettings>
    <add key="SmtpServerHost" value="********" />
    <add key="SmtpServerPort" value="25" />
    <add key="SmtpServerUserName" value="******" />
    <add key="SmtpServerPassword" value="*****" />
</appSettings>

并读为

string smtpHost = ConfigurationManager.AppSettings["SmtpServerHost"];
int smtpPort = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpServerHost"]);

这篇关于我们可以在“ app.config”文件中声明变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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