Use the ADO. NET Component in C # To access the ACCESS database

Source: Internet
Author: User
Tags microsoft access database

Database access is the most important part of all programming languages. C # provides the ADO. NET component for accessing the database. We will start with the most easy-to-use Microsoft Access database and discuss access to the database in C.
The connection object and command object in C # are similar to access, but here we will use another object called adodatareader similar to recordset, it processes recordset objects related to queries.
First, you must use Microsoft Access to create a database. Run access to create a database, but do not create any tables (we will create tables in the following Program .), Save the created database.
Open the ODBC icon in the control panel, click the System DNS tab, select Add> Microsoft Access, and click Finish. In the displayed dialog box, enter the name of the data source, for example, mymdb, create a data source, and double-click OK.
In the following program, we will create a table and insert some values in it.
The program is very simple and intuitive. In the main () function, the adoconnection object obtains the name of the data source from the constructor, and then uses the open () method of adoconenction to open the connection.
After the connection is established, the program will create a table A1 containing two fields. The field name type is character type, and the vno type is integer. The create table command is already placed in the constructor of adocommand. The executenonquery () method is used to execute this query. This method does not return any record sets. Similarly, insert and delete queries can be placed in the constructor of adocommand, so any SQL query can be passed as in VB.
Adodatareader is a new appearance. It is the main object in this program and is responsible for processing the record set returned by adocommand. You can use the xecute () method to view the data returned from the database. The read () method of adodatareader returns a Boolean value. True indicates that the data is in the adodatareader object and the current pointer is moved to the next record of the adodatareader object.
Use Visual Studio. NET β1 to compile the following program code.

Namespace database1
{
Using system;
Using system. Data. Ado;

Public class class1
{
Public class1 ()
{
//
// Add the constructor logic here
//
}

Public static int main (string [] ARGs)
{
Try
{
Adoconnection S = new adoconnection ("Data Source = mymdb ");
S. open ();
Console. writeline ("Connection established ");

// Create a table
Console. Write ("want to create a table? (Y/n )");
String CH = console. Readline ();
If (CH = "Y ")
{
Adocommand createtable = new adocommand ("Create Table A1 (vno integer, name char (20)", S );
Createtable. executenonquery ();
Console. writeline ("aocommand executed/table created ");
}


// Insert values in the table
Console. Write ("want to insert some values in a table? (Y/n )");
Ch = console. Readline ();
If (CH = "Y ")
{
Adocommand instable = new
Adocommand ("insert into A1 values (1, 'Hi')", S );

Instable. executenonquery ();
Console. writeline ("values inserted ");
}

// Delete the entire table
Console. Write ("want to delete all records present in the table? (Y/n )");
Ch = console. Readline ();
If (CH = "Y ")
{
Adocommand deletetable = new adocommand ("delete from A1", S );
Deletetable. executenonquery ();
Console. writeline ("all records deleted from the table ");
}

// View all records
Console. Write ("want to see all the records present in the table/Database (y/n )? ");
Ch = console. Readline ();
If (CH = "Y ")
{
Adocommand allrecs = new adocommand ("select * from A1", S );
Adodatareader R;
Allrecs. Execute (out R );
While (R. Read ())
{
For (INT I = 0; I <R. fieldcount; I ++)
{
Console. Write (R. getvalue (I) + "");
}
Console. writeline ();
}
Console. writeline ("all records displayed ");
R. Close ();
}

S. Close ();
Console. Readline ();
}
Catch (system. Exception E)
{
Console. writeline (E. tostring ());
Console. Readline ();
}

Return 0;
} // End of Main Function
} // End of class
} // End of namespace

 

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.