ASP.NET+DLL+DLL读取配置文件(DLL中访问数据库和WebService)

1 配置文件nwibms.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
     <add key="ConnectionString"

value="server=.;database=ZTIBMS;Trusted_Connection=false;UId=wy;Pwd=123456;"/>
     <add key="WebServiceURL"

value="http://localhost/WYWebService/WebService/TableSearch.asmx"/>
</configuration>
配置文件nwibms.config放在bin下面
2 DLL中获得数据库连接字符串
  首先项目要引用System.Web,才能使用System.Web.HttpContext.Current.Server.MapPath,获得物理路径

 private string GetConnectionString()
        {
            string ret = "";

            string file = System.Web.HttpContext.Current.Server.MapPath("~/bin/nwibms.config");
            try
            {
                using (XmlReader reader = XmlReader.Create(file))
                {
                    while (reader.ReadToFollowing("add"))
                    {
                        string key = reader.GetAttribute("key");

                        if (key == "ConnectionString")
                        {
                            ret = reader.GetAttribute("value");
                        }
                    }
                }
            }
            catch
            {
                ret = "";
            }                    

            return ret;
        }

  3 DLL中获得webservice的url,并更改webservice的url:
    可以在运行时更改webservice的url
     string url = GetUrl();
     localhost.TableSearch TS = new localhost.TableSearch();
     TS.Url = url;

posted on 2008-04-10 14:00  优雅小猪  阅读(2177)  评论(0编辑  收藏  举报

导航