• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >sybase > Connecting to a SQL Anywhere Studio Database Using ADO.NET

Connecting to a SQL Anywhere Studio Database Using ADO.NET

作者:匿名 字体:[增加 减小] 来源:互联网

匿名通过本文主要向大家介绍了connecting,connecting是什么意思,connecting flight,connecting等相关知识,希望本文的分享对您有所帮助

 This document explains how to dynamically create a connection to a SQL Anywhere Studio database through a C# project.Required Software

    • Sybase SQL Anywhere Studio 7.x or later
    • asademo.db file (included with Adaptive Server Anywhere)
    • ASA 8.0 Sample data source (created by default when Adaptive Server Anywhere is installed)
    • Microsoft Visual Studio .NET version 7.0
    • Microsoft ADO.NET
    • Windows NT, 98, 2000, Me, or XP

Steps

  1. Start Visual Studio .NET.
  1. Create a new project. Select Visual C# Projects from the left side.
      • Select Console Application from the right side.
      • Enter the project name CustomerDataReader.
      • Enter the project location: c:\temp.
      • Click OK to close the New Project dialog.
  1. In your code, you must set the  System.Data name space. This is where all the ADO.NET classes are located. Enter the following  using directive at the beginning of your project:
    Using system.Data;
  1. The next required  using directive is the OLE DB .NET Data Provider. Add the following  using directive to your project to use the Microsoft OLE DB .NET provider:
    Using System.Data.OleDb;
    Your source should now look like the following:
    using System; using System.Data; using System.Data.OleDb; namespace CustomerDataReader { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // } } }
    Now you can write the code required to establish communication between Adaptive Server Anywhere and your C# application.
  2. Add the following code to the  public static void Main() function after the  //TODO: Add code to start application here  comment:
//Set your connection string OleDbConnection myConnection =  new OleDbConnection(   @"Data Source=ASA 8.0 Sample;Provider=ASAProv.80"); //open the connection myConnection.Open(); //Creating command object. OleDbCommand myCommand = myConnection.CreateCommand(); //Specify query myCommand.CommandText = "Select fname, lname from Customer";
//DataReader for the command OleDbDataReader myDataReader = myCommand.ExecuteReader(); //Let's display data while ( myDataReader.Read()) { Console.WriteLine("\t{0}\t{1}", myDataReader["fname"],myDataReader["lname"]); } myDataReader.Close(); myConnection.Close();
  1. Run the project by pressing CTRL+F5. You should see the following listing:
         :          :      Dominic Johansen      Stanley Jue      Harry  Jones      Marie  Curie      Elizabeth    Bordon      Len   Manager      Tony   Antolini      Tom   Cruz      Janice  O'Toole      Stevie  Nickolas      Philipe Fernandez      Jennifer     Stutzman      William Thompson Press any key to continue

How does the application work?

  1. OleDbConnection Object
OleDbConnection myConnection =  new OleDbConnection(    @"Data Source=ASA 8.0 Sample;Provider=ASAProv.80");
The OleDbConnection object must be initialized before you can use any other ADO.NET objects. It creates the connection between the application and the database provider. This object requires the provider, in this case ASAProv or ASAProv.80. Then, you have to pass the rest of the connection string, which could be contained in a data source. If the engine is already running, then you only need to pass the user id and password, as follows:  uid=dba;pwd=sql ; The connection string would look similar to the following:
OleDbConnection myConnection =  new OleDbConnection("Provider=ASAProv.80;uid=dba;pwd=sql");
If you need the application to start the engine when you run it without using a DSN, then the connection string would look similar to the following:
OleDbConnection myConnection =  new OleDbConnection(    @"Provider=ASAProv.80;uid=dba;pwd=sql;dbf=c:\temp\dbfile.db");
The @ sign prefacing the connection string allows the backslash in the file name to work; otherwise, double backslashes are necessary to escape the backslash character inside a C# string.
  1. Open Connection Object
myConnection.Open()
This method is required to open the connection between the .NET application and the provider. If this method fails, an exception is thrown.
'System.Data.OleDb.OleDbException' occurred in system.data.dll
  1. SQL Command
    OleDbCommand myCommand = myConnection.CreateCommand(); //Specify query myCommand.CommandText = "Select fname, lname from Customer";
    Once the connection is opened successfully, you can issue a SQL statement. First, a command object must be created to perform database operations. Once the command object is created,  the CommandText property must be set. Since you want to fetch the first name and last name of the customers, you pass the SQL statement to the CommandText property of the Command object. DataReader
    OleDbDataReader myDataReader = myCommand.ExecuteReader();
    The DataReader object is used in this example to quickly get the result of a query. This is a read only object: you cannot update the data. The following code displays the data:
    while ( myDataReader.Read())  { Console.WriteLine("\t{0}\t{1}", myDataReader["fname"],myDataReader["lname"]); }
    The DataReader's read() method allows you to read one row at a time. It returns true as long as there is data to read; otherwise, it returns false.
    myDataReader.Close();
    myConnection.Close();
Finally, you close the DataReader and Connection objects.

 

 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Connecting to a SQL Anywhere Studio Database Using ADO.NET

相关文章

  • 日志问题普及篇
  • 教你学会怎样升级到SQL Anywhere10(10)
  • Sybase的数据类型
  • Sybase数据库轻松日常维护
  • 电信业务系统的Sybase解决方案
  • Sybase数据仓库落户加拿大统计局
  • 在WIN2000上如何配置JISQL环境(for ASE125)
  • Sybase为电话网管值班
  • Sybase应用案例:国信证券
  • 用Sybase advantage清除melinets系统日志

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • 修改Sybase数据库sa密码的方法
    • 携手国家电网 Sybase IQ在电力行业取得突破性进展
    • Sybase数据库的碎片整理
    • Sybase数据库中分离日志与数据文件
    • 如何做 rebuild log
    • 理解Sybase游标的可用性和属性
    • SyBase数据库用户管理的基本概念
    • Sybase应用案例:国信证券
    • Sybase中SQL语言概述
    • .NET中使用oledb连接sybase和csv格式文件的连接

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有