DbConnectionStringBuilder.ConnectionString 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置与 DbConnectionStringBuilder 相关联的连接字符串。
public:
property System::String ^ ConnectionString { System::String ^ get(); void set(System::String ^ value); };
public string ConnectionString { get; set; }
member this.ConnectionString : string with get, set
Public Property ConnectionString As String
属性值
使用 DbConnectionStringBuilder 中包含的键/值对创建的当前连接字符串。 默认值为一个空字符串。
例外
提供的连接字符串参数无效。
示例
以下示例演示 属性的 ConnectionString 可能行为。 示例:
通过将键/值对(一次一个)添加到空 DbConnectionStringBuilder,创建一个连接字符串。
将完整的连接字符串分配给ConnectionString实例的 DbConnectionStringBuilder 属性,并修改字符串中的单个键/值对。
将任意一组键/值对分配给ConnectionString属性 (即,一个与连接字符串) 远程无关的字符串,并修改其中一个值。
向 属性分配无效连接字符串ConnectionString,以演示引发的异常。
注意
该示例包括一个密码以演示 DbConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。
static void Main()
{
// Create a new DbConnctionStringBuilder, and add items
// to the internal collection of key/value pairs.
DbConnectionStringBuilder builder = new
DbConnectionStringBuilder();
builder.Add("Data Source", @"c:\MyData\MyDb.mdb");
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
builder.Add("Jet OLEDB:Database Password", "********");
builder.Add("Jet OLEDB:System Database",
@"c:\MyData\Workgroup.mdb");
// Set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1);
// Display the contents of the connection string, which
// will now contain all the key/value pairs delimited with
// semicolons.
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
// Clear the DbConnectionStringBuilder, and assign a complete
// connection string to it, to demonstrate how
// the class parses connection strings.
builder.Clear();
builder.ConnectionString =
"Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI";
// The DbConnectionStringBuilder class has parsed the contents,
// so you can work with any individual key/value pair.
builder["Data Source"] = ".";
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
// Because the DbConnectionStringBuilder class doesn't
// validate its key/value pairs, you can use this class
// to store any semicolon-delimited list. The following
// snippet places an arbitrary string into the ConnectionString
// property, changes one of the values, and then displays the
// resulting string.
builder.Clear();
builder.ConnectionString =
"Value1=10;Value2=20;Value3=30;Value4=40";
builder["Value2"] = 25;
Console.WriteLine(builder.ConnectionString);
Console.WriteLine();
builder.Clear();
try
{
// Assigning an invalid connection string
// throws an ArgumentException.
builder.ConnectionString = "xxx";
}
catch (ArgumentException)
{
Console.WriteLine("Invalid connection string.");
}
Console.WriteLine();
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
Sub Main()
' Create a new DbConnctionStringBuilder, and add items
' to the internal collection of key/value pairs.
Dim builder As New DbConnectionStringBuilder()
builder.Add("Data Source", "c:\MyData\MyDb.mdb")
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
builder.Add("Jet OLEDB:Database Password", "*******")
builder.Add("Jet OLEDB:System Database", _
"c:\MyData\Workgroup.mdb")
' Set up row-level locking.
builder.Add("Jet OLEDB:Database Locking Mode", 1)
' Display the contents of the connection string, which
' will now contain all the key/value pairs delimited with
' semicolons.
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
' Clear the DbConnectionStringBuilder, and assign a complete
' connection string to it, to demonstrate how
' the class parses connection strings.
builder.Clear()
builder.ConnectionString = _
"Data Source=(local);Initial Catalog=AdventureWorks;" & _
"Integrated Security=SSPI"
' The DbConnectionStringBuilder class has parsed the contents,
' so you can work with any individual key/value pair.
builder("Data Source") = "."
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
' Because the DbConnectionStringBuilder class doesn't
' validate its key/value pairs, you can use this class
' to store any semicolon-delimited list. The following
' snippet places an arbitrary string into the ConnectionString
' property, changes one of the values, and then displays the
' resulting string.
builder.Clear()
builder.ConnectionString = _
"Value1=10;Value2=20;Value3=30;Value4=40"
builder("Value2") = 25
Console.WriteLine(builder.ConnectionString)
Console.WriteLine()
builder.Clear()
Try
' Assigning an invalid connection string
' throws an ArgumentException.
builder.ConnectionString = "xxx"
Catch ex As ArgumentException
Console.WriteLine("Invalid connection string.")
End Try
Console.WriteLine()
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
注解
此属性返回以分号分隔的键/值对列表,这些键/值对存储在 由 DbConnectionStringBuilder维护的集合中。 每对都包含键和值,用等号分隔。 以下示例演示了典型的连接字符串。
"Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=AdventureWorks;Data Source=(local)"
数据提供程序可能需要每个连接字符串属性的特定键和值。 ,这些值单独记录。 类DbConnectionStringBuilder不验证与其连接字符串关联的键/值对,尽管从它继承的类可以。
ConnectionString类的 DbConnectionStringBuilder 属性通常充当一种机制,用于创建和分析用等号分隔的以分号分隔的键/值对列表。 它不提供特定于连接字符串的验证或其他支持。 如果将项添加到集合中 DbConnectionStringBuilder ,则 ConnectionString 属性将反映更改。 如果为 ConnectionString 属性赋值,将 DbConnectionStringBuilder 尝试使用分号和等号分隔符来分析该值。