This document explains how to dynamically create a connection to a SQL Anywhere Studio database through a C# project.
Using system.Data;
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.
//Set your connection stringOleDbConnection myConnection = new OleDbConnection(@"Data Source=ASA 8.0 Sample;Provider=ASAProv.80");//open the connectionmyConnection.Open();//Creating command object.OleDbCommand myCommand = myConnection.CreateCommand();//Specify querymyCommand.CommandText = "Select fname, lname from Customer";//DataReader for the commandOleDbDataReader myDataReader = myCommand.ExecuteReader();//Let's display datawhile ( myDataReader.Read()){Console.WriteLine("\t{0}\t{1}",myDataReader["fname"],myDataReader["lname"]);}myDataReader.Close();myConnection.Close();
::Dominic JohansenStanley JueHarry JonesMarie CurieElizabeth BordonLen ManagerTony AntoliniTom CruzJanice O'TooleStevie NickolasPhilipe FernandezJennifer StutzmanWilliam ThompsonPress any key to continue
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.
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
OleDbCommand myCommand = myConnection.CreateCommand();//Specify querymyCommand.CommandText = "Select fname, lname from Customer";
OleDbDataReader myDataReader = myCommand.ExecuteReader();
while ( myDataReader.Read()){Console.WriteLine("\t{0}\t{1}",myDataReader["fname"],myDataReader["lname"]);}
myDataReader.Close();
myConnection.Close();
Finally, you close the DataReader and Connection objects.
[1] [2] [3] [4] [5] [6] [7] [8] [9] 下一页