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 数据提供程序

 

在 Visual Studio 项目中使用 .NET 数据提供程序

SQL Anywhere .NET 数据提供程序可与 Visual Studio 2005 或更高版本搭配使用开发应用程序。若要使用 SQL Anywhere .NET 数据提供程序,Visual Studio 项目中必须包括以下两项:

  • 对 SQL Anywhere .NET 数据提供程序 DLL 的引用

  • 源代码中引用 SQL Anywhere .NET 数据提供程序类的一行语句

下面详细说明这些步骤。

有关安装和注册 SQL Anywhere .NET 数据提供程序的信息,请参见部署 SQL Anywhere .NET 数据提供程序

在项目中添加对数据提供程序 DLL 的引用

添加引用可通知 Visual Studio 要包含哪些 DLL 以找到 SQL Anywhere .NET 数据提供程序的代码。

♦  在 Visual Studio 项目中添加对 SQL Anywhere .NET 数据提供程序的引用
  1. 启动 Visual Studio 并打开项目。

  2. 在 [Solution Explorer] 窗口中,右击 [References],然后选择 [Add Reference]。

  3. 在 [.NET] 选项卡上,单击 [Browse] 来查找 iAnywhere.Data.SQLAnywhere.dll。请注意,对于每个 Windows 和 Windows Mobile 平台,都提供单独的 DLL 版本。

    • 对于 Windows SQL Anywhere .NET 数据提供程序,缺省位置是 install-dir\Assembly\v2

    • 对于 Windows Mobile SQL Anywhere .NET 数据提供程序,缺省位置是 install-dir\ce\Assembly\v2

  4. 选择 DLL,然后单击 [Open]。

    有关已安装的 DLL 的完整列表,请参见SQL Anywhere .NET 数据提供程序必需的文件

  5. 可以验证 DLL 是否已添加到项目中。打开 [Add Reference] 窗口然后单击 [.NET] 选项卡。随即 iAnywhere.Data.SQLAnywhere.dll 出现在 [Selected Components] 列表中。单击 [OK] 关闭窗口。

    DLL 将添加到项目的 [Solution Explorer] 窗口的 [References] 文件夹中。

在源代码中使用数据提供程序类

为帮助使用 SQL Anywhere .NET 数据提供程序命名空间以及在此命名空间中定义的类型,应在源代码中添加一条指令。

♦  在代码中使用数据提供程序命名空间
  1. 启动 Visual Studio 并打开项目。

  2. 在项目添加下面的指令行:

    • 如果使用的是 C#,请将以下行添加到项目开始处的 using 指令列表中:

      using iAnywhere.Data.SQLAnywhere;
    • 如果使用的是 Visual Basic,则应在项目开头处 Public Class Form1 行之前添加以下行:

      Imports iAnywhere.Data.SQLAnywhere

此指令虽然不是必需的指令,但通过它可使用 SQL Anywhere .NET 类的简写形式。例如:

SAConnection conn = new SAConnection()

在没有此指令的情况下,您仍然可以使用下面的语句:

iAnywhere.Data.SQLAnywhere.SAConnection  
 conn = new iAnywhere.Data.SQLAnywhere.SAConnection()