Connecting to a SQL Anywhere Studio Database Using ADO.NET
2006-06-05 23:10:03 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閹冣挃闁硅櫕鎹囬垾鏃堝礃椤忎礁浜鹃柨婵嗙凹缁ㄧ粯銇勯幒瀣仾闁靛洤瀚伴獮鍥敍濮f寧鎹囬弻鐔哥瑹閸喖顬堝銈庡亝缁挸鐣烽崡鐐嶆棃鍩€椤掑嫮宓佸┑鐘插绾句粙鏌涚仦鎹愬闁逞屽墰閹虫捇锝炲┑瀣╅柍杞拌兌閻ゅ懐绱撴担鍓插剱妞ゆ垶鐟╁畷銉р偓锝庡枟閻撴洘銇勯幇闈涗簼缂佽埖姘ㄧ槐鎾诲礃閳哄倻顦板┑顔硷工椤嘲鐣烽幒鎴旀瀻闁规惌鍘借ⅵ濠电姷鏁告慨顓㈠磻閹剧粯鈷戞い鎺嗗亾缂佸鏁婚獮鍡涙倷閸濆嫮顔愬┑鐑囩秵閸撴瑦淇婇懖鈺冪<闁归偊鍙庡▓婊堟煛鐏炵硶鍋撻幇浣告倯闁硅偐琛ラ埀顒冨皺閺佹牕鈹戦悙鏉戠仸闁圭ǹ鎽滅划鏃堟偨缁嬭锕傛煕閺囥劌鐏犻柛鎰ㄥ亾婵$偑鍊栭崝锕€顭块埀顒佺箾瀹€濠侀偗婵﹨娅g槐鎺懳熺拠鑼舵暱闂備胶枪濞寸兘寮拠宸殨濠电姵纰嶉弲鎻掝熆鐠虹尨宸ョ€规挸妫濆铏圭磼濡搫顫嶇紓浣风劍閹稿啿鐣烽幋锕€绠婚悹鍥у级瀹撳秴顪冮妶鍡樺鞍缂佸鍨剁粋宥夋倷椤掍礁寮垮┑鈽嗗灣閸樠勭妤e啯鍊垫慨妯煎亾鐎氾拷

核心提示: This document explains how to dynamically create a connection to a SQL Anywhere Studio database through a C# project.Required Software Sybase SQ
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
- Start Visual Studio .NET.
- 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.
- 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;
- 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. - 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();
- 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 continueHow does the application work?
- 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.
- 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
- 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.
Tags:Connecting to SQL
编辑录入:coldstar [复制链接] [打 印]- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››sqlserver 每30分自动生成一次
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
赞助商链接