C # connection to Sybase database __ Database

Source: Internet
Author: User
Tags odbc ole sybase sybase client sybase database

C # connection to Sybase database

Preface

Ado.net comes with 4 database client namespaces, one for SQL Server, the other for Oracle, the third for ODBC data sources, and the fourth for implementing databases through OLEBC. If the database is not SQL Server or Oracle, you should use OLE DB, unless you can also use ODBC.

Note: The above quoted from the third edition of "C # Advanced Programming" Tsinghua University Press [ISBN 7-302-10199-x/tp.6975 December 2005 2nd printing]

There are 2 ways to connect C # with Sybase databases.

L through OLEBC

L through ODBC

implemented through OLEBC:

Requires a Sybase OLE DB driver (this can be downloaded online). Specifically implemented as follows:

The first method:

1, first in the project to quote: using System.Data.OleDb;

2. Define a connection string:

Connstr= "Provider=sybase.aseoledbprovider; Server name= "150.1.1.10,8888"; Initial catalog=ismiswebdb; User id=ismiswebdb; Password=ismiswebdba ";

Note:

(1) Server Name: Refers to the IP address and port number of the server, separated by "," in the middle.

(2) Initial Catalog: Is the name of the database server

(3) User Id: is the login name of the database.

(4) Password: Is the password of the database

3. Define a OleDbCommand variable: thiscommand

4. Define a OleDbDataReader variable: thisreader

5, in the need to connect with the data function can be written as follows:

Try

{

OleDbCommand Thiscommand = Thisconnection.createcommand ();

Thiscommand.commandtext = "Select Id,investtype,deptcode,loginname,password,realname,tel,note,rules from Is_user whe Re loginname = ' "+ loginname +" ' and password = ' "+ password +" ' and Rules < 3 ";

OleDbDataReader thisreader = Thiscommand.executereader ();

Loops, creating a linked list structure

while (Thisreader.read ())

{

Cgetuserinfo.id = thisreader[0]. ToString ();

Cgetuserinfo.investtype = thisreader[1]. ToString ();

Cgetuserinfo.deptcode = thisreader[2]. ToString ();

Cgetuserinfo.loginname = thisreader[3]. ToString ();

Cgetuserinfo.password = Thisreader[4]. ToString ();

Cgetuserinfo.realname = thisreader[5]. ToString ();

Cgetuserinfo.tel = Thisreader[6]. ToString ();

Cgetuserinfo.note = Thisreader[7]. ToString ();

Cgetuserinfo.rules = Thisreader[8]. ToString ();

Userinfoarray.add (Cgetuserinfo);

}

Thisreader.close ();

}

Catch

{

}

^_^, is not very simple.

The second method:

1. Open the Syboledb.reg file in the downloaded Sybase OLE DB driver, which is the same path as the installed path (right-click the Syboledb.reg file, select Edit, and view its path).

Note: Of course, you can also do not modify the default value, according to its display path, the download driver (folder Sybase OLE DB) directly in the C: the packing directory. Then double-click the Syboledb.reg file with the left mouse button to register.

1. Open the Sydaadm.exe file under the Sybase OLE DB folder and choose [File]->[new]->[data Source]

2, enter data Source Name (own definition).

3, click the "Set up Data Source" button, enter the server IP address and port number.

To be just in case, click the "Test Connect" button to test the connection. (Of course, the test will also enter the user name, password)

By this, your OLE DB has been set up and users can connect to Sybase using C #.

The code that is specifically implemented in the program is as follows:

String selectcmd = "SELECT * FROM table";

String connstr= "PROVIDER=SYBASE.ASEOLEDBPROVIDER.2;"

+ "Data SOURCE=WEB_PPTC; PASSWORD=WEBPPTCDBA; User ID=WEBPPTC ";

DataAdapter = new OleDbDataAdapter (Selectcmd, CONNSTR);

......

......

The more convenient method is: Your server Name (IP address) and port good can be in Connstri, specifically, please go online search ^_^.

through ODBC

1. Install Sybase client on the server, configure Dsedit, as shown below:

2. Configure ODBC System DSN, as shown in the following figure:

Note: The Data Source name name is on its own, but server name must be consistent with server name from step 1.

At this point, the ODBC configuration has ended and its code in the program is implemented as follows:

String strodbcconn = "Driver={sybase SYSTEM 11}; Srvr=test; UID=WEBPPTC; PWD=WEBPPTCDBA;DB=WEB_PPTC "; WEB_PPTC is the name of the database

OdbcConnection Ologconn;

OdbcCommand Ologcmd;

String strSQL = "Select Dwjm_c,ky_c from jh001";

Ologconn = new OdbcConnection (strodbcconn);

Ologcmd = New OdbcCommand (strSQL, ologconn);

Ologconn.open ();

Mylogdatareader = Ologcmd.executereader (commandbehavior.closeconnection);

while (Mylogdatareader.read ())

{

......

Here, you can get the value of the field queried by the while loop:

For example: String strgetvalue = mylogdatareader[0]. ToString ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.