您的位置:首页 >> 数据库 >> Sybase >> 正文
Sybase RSS
 

Connecting to a SQL Anywhere Studio Database Using ADO.NET

http://www.rdxx.com 06年12月03日 22:42 iTbulo 我要投稿

关键词: Database , connect , ADO.NET , SQL , .NET , ADO , Tab , ASE , NEC
 

 

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

9 7 3 1 2 3 4 8 :


 
 
标签: Database , connect , ADO.NET , SQL , .NET , ADO , Tab , ASE , NEC 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站