如何从配置文件中获取连接字符串-Windows Service App C#4 [英] How to get a connection string from a config file - Windows Service App c# 4
问题描述
大家好,
我正在尝试使用C#4编写的Windows Service项目中的app.config文件检索SQLServer连接字符串.
我排在最前面...
使用System.Configuration;
然后我尝试参加开始活动...
Hi All,
I''m trying to retrieve a SQLServer connection string form my app.config file in my Windows Service project written in C# 4.
I''ve put at the top...
using System.Configuration;
then I try in my start event...
protected override void OnStart(string[] args)
{
try
{
cStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
}
catch (Exception e)
{
File.AppendAllText(Environment.CurrentDirectory + "\\ServiceMessages.txt", "\r\nOn start Error - " + e.Message);
}
}
cStr是之前声明的实例字符串.
但是它不会编译,并且我的聪明才智突出了ConfigurationManager一词,说它在当前上下文中不存在.
我在这里做错什么了?
谢谢.
cStr is an instance string declared earlier.
But it wont compile and my intellisence highlights the ConfigurationManager word saying it doesn''t exist in the current context.
What am I doing wrong here?
Thanks.
推荐答案
您需要使用命名空间System.Configuration ,并且还必须将对引用的引用添加到程序集System.Configuration.dll中.
希望对您有所帮助:)
You need to use the namespace System.Configuration and also have to add the reference to the assembly System.Configuration.dll
hope it helps :)
您是否尝试过定义完整的名称空间:
Have you tried defining the full namespace:
cStr = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString();
或定义
or defining
using System.Configuration;
这篇关于如何从配置文件中获取连接字符串-Windows Service App C#4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!