技术开发 频道

使用 ADOMD.NET 中的连接和会话

  管理会话的示例
  下面的示例演示如何打开连接、创建会话以及关闭连接,同时保持会话在 ADOMD.NET 中处于打开状态:

Public Function CreateSession(ByVal connectionString As String) As String
    
Dim strSessionID As String = ""
    
Dim objConnection As New AdomdConnection

    
Try
        
' First, try to connect to the specified data source.
        ' If the connection string is not valid, or if the specified
        ' provider does not support sessions, an exception is thrown.
        objConnection.ConnectionString = connectionString
        objConnection.Open()

        
' Now that the connection is open, retrieve the new
        ' active session ID.
        strSessionID = objConnection.SessionID
        
' Close the connection, but leave the session open.
        objConnection.Close(False)
        
Return strSessionID

    
Finally
        objConnection
= Nothing
    
End Try
End Function

 

static string CreateSession(string connectionString)
{
    
string strSessionID = "";
    AdomdConnection objConnection
= new AdomdConnection();
    
try
    {
        
/*First, try to connect to the specified data source.
          If the connection string is not valid, or if the specified
          provider does not support sessions, an exception is thrown.
*/
        objConnection.ConnectionString
= connectionString;
        objConnection.Open();

        
// Now that the connection is open, retrieve the new
        
// active session ID.
        strSessionID = objConnection.SessionID;
        
// Close the connection, but leave the session open.
        objConnection.Close(false);
        
return strSessionID;
    }
    
finally
    {
        objConnection
= null;
    }
}
0
相关文章