C#app.config文件connectionStrings中的字符串如何获取本文件中appSettings中设置的字段值
如<appSettings><addkey="server"value="127.0.0.1"/><addkey="database"value="mydatabase"...
如<appSettings>
<add key="server" value="127.0.0.1"/>
<add key="database" value="mydatabase"/>
<add key="username" value="me"/>
<add key="password" value="123"/>
</appSettings>
<connectionStrings>
<add name="Mydata.Properties.Settings.MydataConnectionString"
connectionString="Data Source=127.0.0.1;Initial Catalog=Mydata;Integrated Security=False;User Id=me;pwd=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
我想用connectionString=="Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=False;User Id=" + username + ";pwd=" + password如何实现呢? 展开
<add key="server" value="127.0.0.1"/>
<add key="database" value="mydatabase"/>
<add key="username" value="me"/>
<add key="password" value="123"/>
</appSettings>
<connectionStrings>
<add name="Mydata.Properties.Settings.MydataConnectionString"
connectionString="Data Source=127.0.0.1;Initial Catalog=Mydata;Integrated Security=False;User Id=me;pwd=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
我想用connectionString=="Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=False;User Id=" + username + ";pwd=" + password如何实现呢? 展开
展开全部
string connectionString =
global:: 程序名.Properties.Settings.Default. 连接字符串名;
输入global及双冒号后,会自动提示余下的选择。此方法适用高低版本的C#NET,从2.0到4.0。
下面一个示例,假定程序名是SetAppConfig。
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString =
global::SetAppConfig.Properties.Settings.Default.AdventureWorks2008ConnectionString;
Console.WriteLine(connection.ConnectionString);
}
输出:Data Source=.;Initial Catalog= AdventureWoks2008;Integrated Security=True。
请看我的163博客danyaody,在百度“网页”栏输入“163博客danyaody”即可,第14章例题有读取配置文件连接字符串。
global:: 程序名.Properties.Settings.Default. 连接字符串名;
输入global及双冒号后,会自动提示余下的选择。此方法适用高低版本的C#NET,从2.0到4.0。
下面一个示例,假定程序名是SetAppConfig。
static void Main(string[] args)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString =
global::SetAppConfig.Properties.Settings.Default.AdventureWorks2008ConnectionString;
Console.WriteLine(connection.ConnectionString);
}
输出:Data Source=.;Initial Catalog= AdventureWoks2008;Integrated Security=True。
请看我的163博客danyaody,在百度“网页”栏输入“163博客danyaody”即可,第14章例题有读取配置文件连接字符串。
展开全部
只能在应用程序代码里去组合,配置文件没有这个功能
比如配置文件是:
<appSettings>
<add key="server" value="127.0.0.1"/>
<add key="database" value="mydatabase"/>
<add key="username" value="me"/>
<add key="password" value="123"/>
</appSettings>
<connectionStrings>
<add name="Mydata.Properties.Settings.MydataConnectionString"
connectionString="Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};pwd={3}"
providerName="System.Data.SqlClient" />
</connectionStrings>
代码就是:
string constr = string.Format(ConfigurationManager.ConnectionStrings["Mydata.Properties.Settings.MydataConnectionString"].ConnectionString,
ConfigurationManager.AppSettings["server"],
ConfigurationManager.AppSettings["database"],
ConfigurationManager.AppSettings["username"],
ConfigurationManager.AppSettings["password"]);
比如配置文件是:
<appSettings>
<add key="server" value="127.0.0.1"/>
<add key="database" value="mydatabase"/>
<add key="username" value="me"/>
<add key="password" value="123"/>
</appSettings>
<connectionStrings>
<add name="Mydata.Properties.Settings.MydataConnectionString"
connectionString="Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};pwd={3}"
providerName="System.Data.SqlClient" />
</connectionStrings>
代码就是:
string constr = string.Format(ConfigurationManager.ConnectionStrings["Mydata.Properties.Settings.MydataConnectionString"].ConnectionString,
ConfigurationManager.AppSettings["server"],
ConfigurationManager.AppSettings["database"],
ConfigurationManager.AppSettings["username"],
ConfigurationManager.AppSettings["password"]);
更多追问追答
追问
那在应用程序中如何组合呀? providerName="System.Data.SqlClient"这个字段好像不能组合在连接语句里,有关系吗?
追答
或者config里的Connectionstring删除,直接在代码里用:
string constr = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User Id={2};pwd={3}",
ConfigurationManager.AppSettings["server"],
ConfigurationManager.AppSettings["database"],
ConfigurationManager.AppSettings["username"],
ConfigurationManager.AppSettings["password"]);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["Mydata.Properties.Settings.MydataConnectionString"].ConnectionString;
上面就是了
上面就是了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询