Access different databases in Visual C #

Source: Internet
Author: User

 

Visual C # is the next generation that Microsoft strongly recommends. Program Development language. He has a very important companion-. NET Framework SDK, which encapsulates many class libraries (class libraries ). Visual C # To implement many expansion functions, you must use this partner. The processing of databases in Visual C # is an important manifestation of its functions. Visual C # a namespace in. NET Framework SDK is often used for database processing. It is system. Data. oledb. This namespace encapsulates many classes related to database processing ). This article describes how to access the database in Visual C # through two concrete examples.

1. The program design and running environment are as follows:

Microsoft Windows 2000 Professional Edition,. NET Framework SDK beta 2, Microsoft Access Data component 2.6 (madc2.6)

Ii. Main Functions of the program

This article mainly includes two Source code First, explain how to access the database in Visual C #. The local database selects Microsoft's acess 2000. Second, explain how to access the database in Visual C, the remote database selects SQL Server 7.0, a product of Microsoft.

Iii. Ideas in the example design process

(1) first import the namespace

(2) establish a data connection to the database

(3) create an SQL statement on this data connection to return the required dataset.

(4) Open the data connection, execute the SQL statement, and return the required Dataset

(5) Close the dataset and close the data connection.

4. Example 1: first. cs -- open the local database (My. mdb)

The results after the program runs are shown in:

Important steps in the program:

(1). Import the namespace

In this example, You need to import three namespaces: system, system. Data. oledb, and system. Windows. forms. Import the system namespace because the console class is used in the program. Import the system. Windows. Forms namespace because the application class is used in the program.

(2) establish a connection to the database

To establish a connection, use the class oledbconnection in the system. Data. oledb namespace. Use the following statement to connect to the local database:

String strconnect = "provider = Microsoft. Jet. oledb.4.0; Data Source =" +
Application. startuppath + "\ My. mdb ";
Oledbconnection aconnection = new oledbconnection (strconnect );

The "provider" in the first sentence indicates the type of the database engine. "Data Source" is the name of the database to which it points.

(3) create an SQL statement on this data connection to return the required dataset.

Create an SQL statement to obtain the dataset. Use the class oledbcommand IN THE SYSTEM. Data. oledb namespace. You can use the following statement to complete this task.

Oledbconnection aconnection = new oledbconnection (strconnect );

(4) Open the data connection, execute the SQL statement, and return the required Dataset

To complete this operation, you must use the open method in the oledbconnection class and the executereader method in the oledbcommand class. Another class in the returned dataset's system. Data. oledb namespace-oledbdatareader. This class is like a container that provides the dataset to be accessed. The main statement is as follows:

Aconnection. open ();
Oledbdatareader areader = acommand. executereader ();

(5) Close the dataset and close the data connection.

To close a dataset, you must use the close method in the oledbdatareader class. to close a data connection, you must use the close method in the oledbconnection class. Note: It is best to close the returned dataset before closing the connection to the database. The specific procedure is as follows:

Areader. Close ();
Aconnection. Close ();
(6). An exception is also designed in the program. When an exception occurs, an error message is displayed. Error information is captured through the class oledbexception in the system. Data. oledb namespace. The details are as follows:

Try
{
.......
}
Catch (oledbexception E)
{
Console. writeline ("Error Type:", E. errors [0]. Message );
}
First. CS program source Code As follows:
Using system;
Using system. Data. oledb;
Using system. Windows. forms;
// All namespaces used in the import program
Class oledbtest {
Public static void main ()
{
String strconnect = "provider = Microsoft. Jet. oledb.4.0; Data Source =" +
Application. startuppath + "\ My. mdb ";
Oledbconnection aconnection = new oledbconnection (strconnect );
// Establish a connection to the database
Oledbcommand acommand = new oledbcommand ("select * from persons ",
Aconnection );
// Design the content of the dataset to be returned
Try {
Aconnection. open ();
// Open the connection to the database
Oledbdatareader areader = acommand. executereader ();
// Return the required dataset content
Console. writeline ("The following is all the content of a field in the opened dataset! ");
While (areader. Read ()){
Console. writeline (areader. getstring (0 ));
}
// Display all content of the first field of the dataset. If you want the second field to change "0" to "1"
Areader. Close ();
// Close the dataset
Aconnection. Close ();
// Close the connection to the database
}
Catch (oledbexception E)
{
Console. writeline ("Error Type:", E. errors [0]. Message );
// If an error occurs, the error message is output.
}
}
}

V. Example 2-access a remote database SQL Server 7.0

There are two main differences between accessing SQL Server 7.0 code and accessing acess 2000 Code:

1. The selected database engine is different. To access the connection to the database of SQL Server 7.0, use the following statement:

String strconnect = "provider = sqloledb.1; persist Security info = false; user id = sa; initial catalog = xsgl; Data Source = czdy1 ";

"Initial catalog" is the name of the database to be selected. "Data Source" is the name of the server that provides database services.

2. Do not import the system. Windows. Forms namespace

This is because the remote database is accessed and the application class is not required.

Program source code for accessing the remote database -- second. CS

Using system;
Using system. Data. oledb;
// All namespaces used in the import program
Class oledbtest {
Public static void main ()
{
String strconnect = "provider = sqloledb.1; persist Security info = false
; User id = sa; initial catalog = xsgl; Data Source = czdy1 ";
Oledbconnection aconnection = new oledbconnection (strconnect );
// Establish a connection to the database
Oledbcommand acommand = new oledbcommand ("select * From xsk ",
Aconnection );
// Design the content of the dataset to be returned
Try {
Aconnection. open ();
// Open the connection to the database
Oledbdatareader areader = acommand. executereader ();
// Return the required dataset content
Console. writeline ("The following is all the content of a field in the opened dataset! ");
While (areader. Read ()){
Console. writeline (areader. getstring (0 ));
}
// Display all content of the first field of the dataset. If you want the second field to change "0" to "1"
Areader. Close ();
// Close the dataset
Aconnection. Close ();
// Close the connection to the database
}
Catch (oledbexception E)
{
Console. writeline ("Error Type:", E. errors [0]. Message );
File: // if an error occurs, the error message is output.
}
}
}

Vi. Summary

The preceding two examples show that accessing a remote database is the same as accessing a local database. The main difference is that the data engine required by the database is opened, in programming, the engine parameters required to open a connection to the database are different. Using the two examples above, it is not that difficult for me to access other types of databases.

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.