C # ACCESS Database Operations in BETA2 (1)

Source: Internet
Author: User

Dear friends, it has been 10 days since I went home. I finally started learning. NET, the direct feeling is that the MS help is too bad, a lot of errors on the top, harm me a lot of detours, the results of a lot of things have not been fully done, it is simply!

Compared with BETA1, BETA2 and BETA1 have changed a lot. Currently, books and online materials are basically still on BETA1. This is why many of my friends have encountered many problems during their learning, here I will share some of the problems and experiences I encountered during my learning process with you, hoping to help some of my friends during the learning process!

I guess my friends are learning. NET, the most common problem encountered was dealing with databases! So this time I started to prepare how to operate the database in BETA2. The database adopts our most commonly used ACCESS Database (in fact, it is the most commonly used learning, and in reality I prefer SQL database)

In BETA2 ,. NET provides the following NAMESPACE:
System. Data Namespace
System. Data. OleDb (it is different from BETA1, so it is definitely not possible to run the program in BETA1 to BETA2)

If I want to clarify these things, I don't think I can do it, so I want to use some specific programs to demonstrate the most basic database operations (SELECT, UPDATE, DELETE, INSERT, etc, other things need to be learned by friends!

To operate a database, you must first open the database. The following uses an ACCESS database as an example to illustrate how to open a database connection! Here we need to use: System. Data. OleDb. OleDbConnection class! (If you operate SQL databases, we 'd better use the System. Data. SqlClient. SqlConnection class)

I will first write my own program:

Using System. Data
Using System. Data. OleDb

Public OleDbConnection getConn ()
{
String connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = F: \ web \ notesbook \ class \ leavenotes. mdb ";
OleDbConnection tempconn = new OleDbConnection (connstr );
Return (tempconn );
}

I believe that anyone who has used ADO can understand it! First, we define a variable of the String type, which stores the connection String that we connect to the database, and then define a System. data. oleDb. oleDbConnection type object, instantiate it, and finally return this object! It should be noted that I didn't put the statement: tempconn. Open (); into this function. The reason is that I will explain it later. Here I just want to remind you!

Through the above function, we have obtained a Connection object similar to the Connection object in ADO! The following is the specific database operation!

Before specific operations, I think it is necessary to first understand the following two classes:
System. Data. OleDb. OleDbDataAdapter
System. Data. OleDb. OleDbDataReader

System. Data. OleDb. OleDbDataAdapter: You can directly contact DataSet and operate the Data source. It is more powerful and consumes System resources!
System. data. oleDb. oleDbDataReader: It is similar to the read-only forward record set in ADO. It is most commonly used when only the data needs to be read and displayed in sequence, compared with System. data. oleDb. oleDbDataAdapter is short of system resources! In fact, OleDbDataReader can implement all functions, but we should try to use the former from the perspective of resource usage! However, some functions can be implemented only by using OleDbDataAdapter!


* SELECT operation!
The following figure shows how OleDbDataReader and OleDbDataAdapter select records from the database:

// Obtain the details of the current message by ID. Use the STRING type parameter
Public Notebook getNoteFromID (string noteid)
{
Notebook tempnote = new Notebook (); // defines the return value.

Try
{
OleDbConnection conn = getConn (); // getConn (): Get the connection object
String strCom = "Select * from notes where id =" + noteid;
OleDbCommand myCommand = new OleDbCommand (strCom, conn );
Conn. Open ();
OleDbDataReader reader;
Reader = myCommand. ExecuteReader (); // execute the command and obtain the corresponding DataReader
// Assign the value to the tempnote object
If (reader. Read ())
{
Tempnote. id = (int) reader ["id"];
Tempnote. title = reader ["title"]. ToString ();
Tempnote. content = reader ["content"]. ToString ();
Tempnote. author = reader ["author"]. ToString ();
Tempnote. email = reader ["email"]. ToString ();
Tempnote. http = reader ["http"]. ToString ();
Tempnote. pic = reader ["pic"]. ToString ();
Tempnote. hits = (int) reader ["hits"];
Tempnote. posttime = (DateTime) reader ["posttime"];
}
Else // if this record does not exist, an error is thrown!
{
Throw (new Exception ("this record does not exist currently! "));
}

Reader. Close ();
Conn. Close ();
}
Catch (Exception e)
{
// Throw (new Exception ("Database Error:" + e. Message ));
}
Return (tempnote); // return the Databook object
}

The preceding program uses OleDbDataReader to obtain specific records! The statements used are written as follows:
OleDbConnection conn = getConn (); // getConn (): Get the connection object
String strCom = "Select * from notes where id =" + noteid; // SQL statement
OleDbCommand myCommand = new OleDbCommand (strCom, conn); // create an OleDbCommand object
Conn. Open (); // note that the Open statement I mentioned earlier is used here!
OleDbDataReader reader;
Reader = myCommand. ExecuteReader (); // execute the command and obtain the corresponding result.

I have added a description after each sentence, in which OleDbConnection conn = getConn () is to get the database connection through the getConn function I mentioned earlier. There is nothing to say about other statements, they are all very simple, so I will not say much!


I will list another routine for getting records through OleDbDataAdapter:
// Getlist (): gets the list of messages currently needed.
Public DataView getNoteList ()
{
DataView dataview;
System. Data. DataSet mydataset; // defines DataSet.

Try
{
OleDbConnection conn = getConn (); // getConn (): Get the connection object
OleDbDataAdapter adapter = new OleDbDataAdapter ();
String sqlstr = "select * from notes order by posttime desc ";
Mydataset = new System. Data. DataSet ();
Adapter. SelectCommand = new OleDbCommand (sqlstr, conn );
Adapter. Fill (mydataset, "notes ");
Conn. Close ();
}
Catch (Exception e)
{
Throw (new Exception ("Database Error:" + e. Message ));
}
Dataview = new DataView (mydataset. Tables ["notes"]);
Return (dataview );
}


This program may be somewhat complicated. Similarly, I should first list the key statements and describe them:

OleDbConnection conn = getConn (); // obtain the connection object through the getConn () function
OleDbDataAdapter adapter = new OleDbDataAdapter (); // instantiate OleDbDataAdapter object
String sqlstr = "select * from notes order by posttime desc"; // SQL statement

Mydataset = new System. data. dataSet (); // because OleDbDataAdapter needs to be used with DataSet, the DataSet object is defined here. In fact, OleDbDataAdapter is complex, in fact, because of DataSet, DataSet is somewhat similar to the recordset object in ADO, but its function far exceeds it, and it is disconnected from the database and can store multiple record sets!

Adapter. SelectCommand = new OleDbCommand (sqlstr, conn); // you can specify

Adapter. Fill (mydataset, "notes"); // execute and add the result to the "notes" table in mydataset.
Conn. Close (); // Close the connection!

Add some additional instructions to the above program. Because getNoteLista obtains a series of records and uses the control DataGrid for paging display, I return a DataView type object!

This article is not written by me, but I forgot where to download it. So I wrote the original BR>

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.