2011-10-26 137 views
19

如何從具有.NET函數的app.config中的這樣的connectionString獲取用戶和密碼?從ConnectionStringSettings獲取用戶和密碼

當然,我可以讀取該字符串並獲取ID =和Password =後的值。

<connectionStrings> 
<add name="MyConString" connectionString="Data Source=(local);Initial Catalog=MyDatabase;Persist Security Info=True;User ID=MyUsername Password=MyPassword;Connect providerName="System.Data.SqlClient"/>  
</connectionStrings> 
+1

我希望你注意到你缺少一個「;名爲myUsername」後‘’ - 前‘密碼’ –

回答

31

使用與

string connString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString; 

的ConnectionBuilderClass

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder("Your connection string"); 
string password = builder.Password; 


共同實現這一目標。

+0

+1絕對是最簡單的解決方案 – Tim

+0

!燁大答案 – msfanboy

-2
var connString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString; 
var tokens = connString.Split(';'); 

string userId; 
string password; 

for(var i = 0; i < tokens.Length; i++) { 
    var token = tokens[i]; 
    if(token.StartsWith("User ID")) 
     userId = token.Substring(token.IndexOf("=") + 1); 

    if(token.StartsWith("Password")) 
     password = token.Substring(token.IndexOf("=") + 1); 
} 
+0

+1的代碼示例但是,應該不會是'token.Substring(token.IndexOf( 「=」)+ 1);'去掉「=」? – Tim

+0

@Tim:謝謝,注意和編輯 - 脫口而出總是討厭它的醜陋的頭:) –

1

您可以從以下

SqlConnectionStringBuilder yourconn = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); 
string password = yourconn.Password; 

獲得連接字符串,然後你可以得到你正在尋找的子字符串。

2
var builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString) 
var user = builder.UserID; 
var password = builder.Password; 
4
SqlConnectionStringBuilder con = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); 
string myUser = con.UserID; 
string myPass = con.Password; 
-2
string connectionString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString; 
var tokens = connectionString.Split(';').Select(n => n.Split('='); 
string userId = tokens.First(n => n[0].Equals("User ID").Select(n => n[1]); 
string password = tokens.First(n => n[0].Equals("Password").Select(n => n[1]); 
0

只是一個位添加到托馬斯Walek的答案。

只有在連接字符串中的「用戶ID」正確大寫時,此方法纔有效。 Oracle提供商接受「用戶ID」確定,但SqlConnectionStringBuilder沒有工作。使用System.Configuration ;:

public static class DbConnectionFactory 
{ 
    public static ConnectionStringSettings AppConnectionSettings = ConfigurationManager.ConnectionStrings["{A connection string name}"]; 
    public static SqlConnectionStringBuilder AppConnBuilder = new SqlConnectionStringBuilder(AppConnectionSettings.ConnectionString); 

    public static string DbUserID 
    { 
     get 
     { 
      return AppConnBuilder.UserID; 
     } 
     set { } 
    } 
} 
0

添加到System.Configuration的引用,然後使用

string MyDBConnection = ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString; 
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(MyDBConnection); 
string UserID = builder.UserID; 
string Password = builder.Password; 
string ServerName = builder.DataSource; 
string DatabaseName = builder.InitialCatalog; 
0
public static string GetConnectionSettings(string searchSetting) 
{ 
    var con = ConfigurationManager.ConnectionStrings["yourConnectionHere"]‌​.ConnectionString; 
    String[] myString = con.Split(';'); 
    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    for (int i = 0; i < myString.Count(); i++) 
    { 
     String[] con3 = myString[i].Split('='); dict.Add(con3[0], con3[1]); 
    } 

    return dict[searchSetting]; 
} 

爲searchSetting你可以使用你想要的 「用戶是」 或密碼。