2004-05-18 Liu Yanqing Computer World
Use the ADO.NET components in C # to access the Access database
Database Access is the most important part in all programming languages, and C # provides ADO.NET components to access databases. We will discuss access from the easiest and easy-to-use Microsoft Access database.
The Connection object and the Command object in the C # are similar to Access, but we will also use other objects similar to the Recordset similar to the Recordset, which is responsible for processing the Recordset object related to the query.
First, you must create a database using Microsoft Access. Run Access, create a database, but don't create any tables (we will create a table in the program below.), Save the created database.
Open the ODBC icon in the control panel, click the System DNS tab, select Add> Microsoft Access, and click the Finish button. Enter the name of the data source in the dialog that pulls down, such as mymdb, then create a data source, double-click the OK button.
In the following program, we will create a table and insert some values.
The program is very simple and intuitive. In the main () function, the Adoconnection object takes the name of the data source into the constructor and then opens the connection using the AdoconAnction Open () method.
After the connection is created, the program will create table A1 containing two fields, where the type of field name is characteristic, and the type of VNO is integer. The CREATE TABLE command has been placed in the AdoCommand constructor, and the executenonquery () method is used to perform this query, which does not return any record set. Similarly, INSERT and DELETE queries can also be placed in the CONSTRUctor of AdoCommand, so you can pass any SQL queries like it in VB.
AdodaReader is new, it is the main object in this segment, responsible for handling the record set returned by AdoCommand. Using the Xecute () method, you can see the data returned from the database. The read () method of AdodataReader returns the value of the Boolean value, True marks the data in the AdodataReader object, and moves the current pointer to the next record of the AdodaAder 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 constructor's logic here
//
}
Public static int main (String [] ARGS)
{
Try
{
Adoconnection S = New Adoconnection ("Data Source = My MDB);
S.Open ();
Console.writeline ("Connection Establish");
// 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 ("AocMand Executed / Table Created);
}
// Insert the value 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 Deleded from the Table");
}
/ / See all records
Console.Write ("Want TO SEE ALL THE RECORDS PRESENT IN TABLE / DATABASE (Y / N)?");
CH = console.readline ();
IF (ch == "y")
{
Adocommand AllRecs = New Adocommand ("SELECT * A1", S);
AdodataReader R;
AllRecs.execute (OUT R);
While (R.Read ())
{
For (int i = 0; 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; } // main function end } // Class end } // End of the namespace