OleDbConnectionStringBuilder.Keys 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取包含 ICollection 中的键的 OleDbConnectionStringBuilder。
public:
virtual property System::Collections::ICollection ^ Keys { System::Collections::ICollection ^ get(); };
public override System.Collections.ICollection Keys { get; }
member this.Keys : System.Collections.ICollection
Public Overrides ReadOnly Property Keys As ICollection
属性值
一个 ICollection,它包含 OleDbConnectionStringBuilder 中的键。
示例
以下控制台应用程序示例新建一个 OleDbConnectionStringBuilder。 该代码循环访问显示键/值对的 ICollection 属性返回的 Keys。
注意
该示例包括一个密码以演示 OleDbConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。
using System.Data.OleDb;
class Program
{
static void Main()
{
try
{
// Build an empty instance, just to see
// the contents of the keys.
DumpBuilderContents("");
// Create a SQL Server connection string.
DumpBuilderContents("Provider=sqloledb;Data Source=(local);" +
"Initial Catalog=AdventureWorks;" +
"User Id=ab;Password=Password@1");
// Create an Access connection string.
DumpBuilderContents("Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data Source=C:\Sample.mdb");
// Create an Oracle connection string.
DumpBuilderContents("Provider=msdaora;Data Source=SomeOracleDb;" +
"User Id=userName;Password=Pass@word1;");
// Create a Sybase connection string.
DumpBuilderContents("Provider=ASAProv;Data source=myASA");
Console.WriteLine("Press any key to finish.");
Console.ReadLine();
}
catch (System.ArgumentException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static void DumpBuilderContents(string connectString)
{
OleDbConnectionStringBuilder builder =
new OleDbConnectionStringBuilder(connectString);
Console.WriteLine("=================");
Console.WriteLine("Original connectString = " + connectString);
Console.WriteLine("builder.ConnectionString = " + builder.ConnectionString);
foreach (string key in builder.Keys)
{
Console.WriteLine(key + "=" + builder[key].ToString());
}
}
}
Imports System.Data.OleDb
Module Module1
Sub Main()
Try
' Build an empty instance, just to see
' the contents of the keys.
DumpBuilderContents("")
' Create a SQL Server connection string.
DumpBuilderContents("Provider=sqloledb;Data Source=(local);" & _
"Initial Catalog=AdventureWorks;" & _
"User Id=ab;Password=Password@1")
' Create an Access connection string.
DumpBuilderContents("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Sample.mdb")
' Create an Oracle connection string.
DumpBuilderContents("Provider=msdaora;Data Source=SomeOracleDb;" & _
"User Id=userName;Password=Pass@word1;")
' Create a Sybase connection string.
DumpBuilderContents("Provider=ASAProv;Data source=myASA")
Console.WriteLine("Press any key to finish.")
Console.ReadLine()
Catch ex As System.ArgumentException
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
Private Sub DumpBuilderContents(ByVal connectString As String)
Dim builder As New OleDbConnectionStringBuilder(connectString)
Console.WriteLine("=================")
Console.WriteLine("Original connectString = " & connectString)
Console.WriteLine("builder.ConnectionString = " & builder.ConnectionString)
For Each key As String In builder.Keys
Console.WriteLine(key & "=" & builder.Item(key).ToString)
Next
End Sub
End Module
注解
ICollection 中的值的顺序未指定,但与 ICollection 属性返回的 Values 中的关联值顺序相同。