2011-02-28 57 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; } 
} 
相关问题