2012-12-06 2 views
0

.net api를 통해 프로그래밍 방식으로 web.config 연결 문자열에서 서버 이름과 데이터베이스 이름을 모두 검색하는 방법은 무엇입니까? 가급적이면 html 또는 xml 파서를 사용하지 않고 만듭니다. 이러한 유형의 정보를 검색하는 가장 간단한 방법을 찾고 있습니다. 예 Web.config의 조각 :web.config의 연결 문자열에서 서버 이름과 데이터베이스 이름을 모두 검색하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <connectionStrings> 
    <add name="ConnectionString" connectionString="Provider=SQLOLEDB;Data Source=MyServer;User ID=admind;[email protected]_Fak3;Initial Catalog=dbDatabase" providerName="System.Data.OLEPlethora"/> 
    </connectionStrings> 
<system.web> 

결과 : 불러 오는 연결 문자열 설정에 관한 서버 = MyServer를 데이터베이스 = dbDatabase

찾을 수이 link. 이것은 훨씬 더 단순한 것처럼 보이지만 많은 연결 문자열을 반복 할 수 있는지는 잘 모르겠습니다.

답변

3

같은 설정에서 ConnectionString을 가져 오기 :

var myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 

그런 다음 관련 부분을 어떤 ConnectionString을 구문 분석하고 추출하는 oledb connectionstring builder를 사용할 수 있습니다.

var builder = new System.Data.OleDb.OleDbConnectionStringBuilder(myConnectionString); 
var servername = builder["Data Source"]; 
var database = builder["Initial Catalog"]; 

Console.WriteLine("server={0}, database={1}", servername, database); 
관련 문제