Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - 编程 » SQL Anywhere 数据访问 API » SQL Anywhere .NET 数据提供程序 » 连接到数据库

 

检查连接状态

一旦应用程序与数据库建立了连接,您就可以检查连接状态,确保在从数据库读取数据以进行更新之前连接处于打开状态。如果连接丢失或被占用,或者正在处理另一语句,可以将适当的消息返回给用户。

SAConnection 类具有一个可用于检查连接状态的状态属性。可能的状态值为 Open 和 Closed。

以下代码检查 Connection 对象是否已初始化,如果已初始化,则确保连接处于打开状态。如果连接没有打开,则会为用户返回一条消息。

if( _conn == null || _conn.State !=
  ConnectionState.Open ) {
  MessageBox.Show( "Connect to a database first",
   "Not connected" );
  return;

有关详细信息,请参见State 属性