ConnectionStringSettingsCollection.IndexOf(ConnectionStringSettings) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回传递的 ConnectionStringSettings 对象的集合索引。
public:
int IndexOf(System::Configuration::ConnectionStringSettings ^ settings);
public int IndexOf (System.Configuration.ConnectionStringSettings settings);
member this.IndexOf : System.Configuration.ConnectionStringSettings -> int
Public Function IndexOf (settings As ConnectionStringSettings) As Integer
参数
- settings
- ConnectionStringSettings
集合中的 ConnectionStringSettings 对象。
返回
指定的 ConnectionStringSettingsCollection 对象的集合索引。
示例
以下示例演示如何获取指定 ConnectionStringSettings 对象的索引。
static void GetIndex()
{
try
{
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Clear the connection strings collection.
ConnectionStringsSection csSection =
config.ConnectionStrings;
ConnectionStringSettingsCollection csCollection =
csSection.ConnectionStrings;
// Get the connection string setting element
// with the specified name.
ConnectionStringSettings cs =
csCollection["ConnStr0"];
// Get its index;
int index = csCollection.IndexOf(cs);
Console.WriteLine(
"Connection string settings index: {0}",
index.ToString());
}
catch (ConfigurationErrorsException err)
{
Console.WriteLine(err.ToString());
}
}
Shared Sub GetIndex()
Try
Dim config _
As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration( _
ConfigurationUserLevel.None)
' Clear the connection strings collection.
Dim csSection _
As ConnectionStringsSection = _
config.ConnectionStrings
Dim csCollection _
As ConnectionStringSettingsCollection = _
csSection.ConnectionStrings
' Get the connection string setting element
' with the specified name.
Dim cs _
As ConnectionStringSettings = _
csCollection("ConnStr0")
' Get its index;
Dim index As Integer = _
csCollection.IndexOf(cs)
Console.WriteLine( _
"Connection string settings index: {0}", _
index.ToString())
Catch err As ConfigurationErrorsException
Console.WriteLine(err.ToString())
End Try
End Sub