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 数据提供程序 » 使用 Table Viewer 代码示例

 

了解 Table Viewer 示例项目

本节通过介绍 Table Viewer 代码示例中的部分代码,来说明 SQL Anywhere .NET 数据提供程序的一些主要功能。Table Viewer 项目使用 SQL Anywhere 示例数据库 demo.db,该数据库位于您的 SQL Anywhere 示例目录中。

有关 SQL Anywhere 示例目录位置的信息,请参见示例目录

有关示例数据库的信息(包括数据库中的表和它们之间的关系),请参见SQL Anywhere 示例数据库

本节以一次多行的形式来介绍代码。此处并未包括示例中的所有代码。要查看所有代码,请打开 samples-dir\SQLAnywhere\ADO.NET\TableViewer 中的示例项目。

声明控件   下面的代码声明名为 label1 和 label2 的两个标签、名为 txtConnectString 的 TextBox、名为 btnConnect 的按钮、名为 txtSQLStatement 的 TextBox、名为 btnExecute 的按钮,以及名为 dgResults 的 DataGrid。

private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtConnectString;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.TextBox txtSQLStatement;
private System.Windows.Forms.Button btnExecute;
private System.Windows.Forms.DataGrid dgResults;

声明连接对象   SAConnection 类型用于声明未初始化的 SQL Anywhere 连接对象。SAConnection 对象用于表示与 SQL Anywhere 数据源的唯一连接。

private SAConnection _conn;

有关 SAConnection 类的详细信息,请参见SAConnection 类

连接到数据库   txtConnectString 对象的 Text 属性的缺省值为 "Data Source=SQL Anywhere 11 Demo"。应用程序用户可通过向 txtConnectString 文本框键入新值来替换此值。可通过打开标签为 [Windows Form Designer Generated Code] 的 TableViewer.cs 内的区域或部分,来查看此缺省值是如何设置的。本节中,您会发现以下代码行。

this.txtConnectString.Text = "Data Source=SQL Anywhere 11 Demo";

随后,SAConnection 对象使用该连接字符串连接到数据库。以下代码使用 SAConnection 构造函数创建带有连接字符串的新连接对象。它随后会使用 Open 方法建立连接。

_conn = new SAConnection( txtConnectString.Text );
_conn.Open();

有关 SAConnection 构造函数的详细信息,请参见SAConnection 成员

定义查询   txtSQLStatement 对象的 Text 属性的缺省值为 "SELECT * FROM Employees"。应用程序用户可通过向 txtSQLStatement 文本框键入新值来替换此值。

使用 SACommand 对象来执行 SQL 语句。以下代码使用 SACommand 构造函数声明并创建一个命令对象。此构造函数接受表示要执行的查询的字符串,以及表示在其上执行查询的连接的 SAConnection 对象。

SACommand cmd = new SACommand( txtSQLStatement.Text.Trim(), 
                                           _conn );

有关 SACommand 对象的详细信息,请参见SACommand 类

显示结果   使用 SADataReader 对象来获取查询的结果。以下代码使用 ExecuteReader 构造函数声明并创建一个 SADataReader 对象。此构造函数是以前声明的 SACommand 对象 cmd 的成员。ExecuteReader 将命令文本发送到连接以用于执行并构建一个 SADataReader。

SADataReader dr = cmd.ExecuteReader();

下面的代码将 SADataReader 对象连接到 DataGrid 对象,这样会使结果列出现在屏幕上。随后会关闭 SADataReader 对象。

dgResults.DataSource = dr;
dr.Close();

有关 SADataReader 对象的详细信息,请参见SADataReader 类

错误处理   如果应用程序尝试连接数据库或者填充 [Tables] 组合框时发生错误,以下代码将捕获错误并显示其消息:

try {
  _conn = new SAConnection( txtConnectString.Text );
  _conn.Open();

  SACommand cmd = new SACommand(
    "SELECT table_name FROM SYS.SYSTAB where creator = 101", _conn );
  SADataReader   dr = cmd.ExecuteReader();

  comboBoxTables.Items.Clear();
  while ( dr.Read() ) {
    comboBoxTables.Items.Add( dr.GetString( 0 ) );
  }
  dr.Close();
} catch( SAException ex ) {
  MessageBox.Show( ex.Errors[0].Source + " : " +
       ex.Errors[0].Message + " (" +
       ex.Errors[0].NativeError.ToString() + ")",
       "Failed to connect" );
}

有关 SAException 对象的详细信息,请参见SAException 类