C # ACCESS Database Operations in BETA2 (2)

Source: Internet
Author: User

Last time I talked about how to do this in ADO. . NET to execute the SELECT statement. This time, we will look at how to execute the statements such as DELETE, UPDATE, and INSERT.

We can also see through examples this time, in which we use the System. Data. OleDb. OleDbCommand class. In fact, we also used the SELECT class before execution!

Below I write my program:

// Modify the specific data in the message book
Public Boolean UpdateNote (Notebook note)
{
Boolean tempvalue = false;
String sqlstr = ""; // It was defined here at the time to check whether My SQL statement is correct when an exception occurs.
Try
{
// Use the database connection function I wrote earlier.
OleDbConnection conn = getConn (); // getConn (): Get the connection object,
Conn. Open ();

// Determine the SQL statement we need to execute. This is the UPDATE statement!
Sqlstr = "UPDATE notes SET ";
Sqlstr + = "title =" + note. title + ",";
Sqlstr + = "content =" + DealString (note. content) + ",";
Sqlstr + = "author =" + note. author + ",";
Sqlstr + = "email =" + note. email + ",";
Sqlstr + = "http =" + note. http + "";
// Sqlstr + = "pic =" + note. pic + "";
Sqlstr + = "where id =" + note. id;

// Define the command object and execute the corresponding SQL statement
OleDbCommand myCommand = new OleDbCommand (sqlstr, conn );
MyCommand. ExecuteNonQuery (); // when executing the SELECT statement, we use ExecuteReader ()
Conn. Close ();


// If the execution is successful, TRUE is returned. Otherwise, FALSE is returned.
Tempvalue = true;
Return (tempvalue );
}
Catch (Exception e)
{
Throw (new Exception ("database update error:" + sqlstr + "" + e. Message ));
}
}

In this example, the UPDATE operation is performed on records with a specific ID. I have written all the explanations in the program. The database-related statements are those in try!

In fact, we can also execute INSERT and DELETE operations in the above mode. Below I will list my programs below!

/* Delete a specific record and delete a field through the string type ID. In my program, I overload this function, in this way, you can use the ID parameter of the INT type to delete a specific field */
Public Boolean DelNote (string delid)
{
Boolean tempvalue = false;
String sqlstr = "";
// Connect to the database
Try
{
OleDbConnection conn = getConn (); // getConn (): Get the connection object
Conn. Open ();

Sqlstr = "delete * from notes where id =" + delid;

// Define the command object and execute the corresponding SQL statement
OleDbCommand myCommand = new OleDbCommand (sqlstr, conn );
MyCommand. ExecuteNonQuery ();
Conn. Close ();


// If the execution is successful, TRUE is returned. Otherwise, FALSE is returned.
Tempvalue = true;
Return (tempvalue );
}
Catch (Exception e)
{
Throw (new Exception ("database update error:" + sqlstr + "" + e. Message ));
}
}

Careful friends should be able to see that, in fact, this program is different from the preceding SQL statement, and the others are basically the same! Similarly, we can use this method when inserting a new record in the database. The procedure is as follows:
// Add data to the Message book
Public Boolean AddNote (Notebook note)
{

Boolean tempvalue = false; // defines the return value and sets the initial value.
// Add the data in the note to the database below!
Try {

OleDbConnection conn = getConn (); // getConn (): Get the connection object
Conn. Open ();

// Set SQL statements
String insertstr = "insert into notes (title, content, author, email, http, pic, hits, posttime) VALUES (";
Insertstr + = note. title + ",";
Insertstr + = DealString (note. content) + ",";
Insertstr + = note. author + ",";
Insertstr + = note. email + ",";
Insertstr + = note. http + ",";
Insertstr + = note. pic + ",";
Insertstr + = note. hits + ",";
Insertstr + = note. posttime + ")";

OleDbCommand insertcmd = new OleDbCommand (insertstr, conn );
Insertcmd. ExecuteNonQuery ();

Conn. Close ();
Tempvalue = true;
}
Catch (Exception e)
{
Throw (new Exception ("Database Error:" + e. Message ));
}
Return (tempvalue );
}

// Process data. Block the dangerous characters before saving the data to the database!
Public string DealString (string str)
{
Str = str. Replace ("<", "<");
Str = str. Replace (">", "> ");
Str = str. Replace ("", "<br> ");
Str = str. Replace ("","'");
Str = str. Replace ("x0020 ","");

Return (str );
}

// Recover data: restores the data in the database to the original state before processing.
Public string UnDealString (string str)
{
Str = str. Replace ("<", "<");
Str = str. Replace (">", "> ");
Str = str. Replace ("<br> ","");
Str = str. Replace ("'","");
Str = str. Replace ("", "x0020 ");

Return (str );
}

I have listed two functions UnDealString () and DealString () at the same time. They do some advance processing and restoration work with the input content!

These programs are simple, so I will not talk much about them!
In fact, my database operations are just ADO. And I have not studied it carefully by using DataSet. So I cannot write anything. I will be ready to take a good look at it in the next few days, at that time, I will share my feelings with you!

In addition, the programs I used earlier are all used when I wrote a test program in the message book! If you are interested, I will post all my learning code!

Now, I want to start my business! Goodbye next time!

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.