2011-02-28 56 views
1

我有一個帶密碼的數據庫。 如果我允許使用連接字符串保存密碼,它將在.config文件中可見。如何在運行時將密碼提供給數據集?

如何在運行時設置相同的設置?

經過:

Point ADO.Net DataSet to different databases at runtime?

Changing dataset connection string at runtime

但需要一些方法來改變的ConnectionString中設置本身,以避免在許多地方的變化。

更新:這是Windows窗體應用程序。

回答

2

這可以通過覆蓋以下屬性來實現。 步驟。

  1. 轉到設置

  2. 點擊查看代碼

  3. 鑑於代碼添加以下代碼

Vb時,淨

Default Public Overrides Property Item(ByVal propertyName As String) As Object 
    Get 
     If propertyName = "MyConnectionString" Then 
      Return MyBase.Item(propertyName) & ";Password=Yourpassword;" 
     End If 
     Return MyBase.Item(propertyName) 
    End Get 
    Set(ByVal value As Object) 
     MyBase.Item(propertyName) = value 
    End Set 
End Property 

C#(Roug

myDataSet myData = new myDataSet(); 

myDataSetTableAdapters.myTableAdapter myInfo = 
    new myDataSetTableAdapters.myTableAdapter(); 

myInfo.Connection.ConnectionString += ";Password=myPassword"; 
myInfo.Fill(myData.Info); 

您可以自由設置中的TableAdapter的ConnectionString的任何部分:使用代碼轉換器)

public override object this[string propertyName] { 
    get { 
     if (propertyName == "MyConnectionString") { 
      return base.Item(propertyName) + ";Password=Yourpassword;"; 
     } 
     return base.Item(propertyName); 
    } 
    set { base.Item(propertyName) = value; } 
} 
1

通過使用DataSet,我使用下面的代碼H代碼轉換。

0

功能C#代碼,剛剛測試(基於@Sachin Chavan的精彩回答)。覆蓋必須放置在Settings.cs文件中:

public override object this[string propertyName] 
{ 
    get 
    { 
     if (propertyName == "nameOfYourConnectionStringProperty") 
     { 
      return base[propertyName].ToString().Replace("******", "Y0uRpA$sW0rD"); 
     } 
     return base[propertyName]; 
    } 
    set { base[propertyName] = value; } 
} 
相關問題