C # simple steps for adding, deleting, querying, and modifying Access databases

Source: Internet
Author: User

Reference set: using System. data. oleDb; static string exePath = System. environment. currentDirectory; // path of the program // create a connection object OleDbConnection conn = new OleDbConnection ("provider = Microsoft. jet. OLEDB.4.0; data source = "+ exePath + @" \ file name. mdb "); 1. for example, table data (including "refresh" and "connect to Database") and conditional query are all related to query. OleDbDataAdapter private void is used to obtain data tables/queries () {conn. open (); // obtain the data table // string SQL = "select * from table name order by field 1"; // query string SQL = "s Elect * from table name where field 2 = "...; oleDbDataAdapter da = new OleDbDataAdapter (SQL, conn); // create an adaptation object DataTable dt = new DataTable (); // create a table object da. fill (dt); // use an adaptation object to Fill in the table object dataGridView1.DataSource = dt; // use the table object as the data source conn of the DataGridView. close ();} Where "get data table" is a method frequently called by nesting, so remove the conn at the beginning and end. open and Close, so as not to accumulate with Open in other methods and thus report an error. 2. oleDbCommand private void is used to add, delete, and modify table data. open (); // Add string SQL = "insert into Table Name (Field 1, Field 2, Field 3, field 4) values (...) "; // delete // string SQL =" delete from table name where field 1 = "...; // modify // string SQL = "update student set student ID = "...; oleDbCommand comm = new OleDbCommand (SQL, conn); comm. executeNonQuery (); conn. close ();} Where ExecuteNonQuery successfully changed the number of tuples, so comm. executeNonQuery () can also be transformed to judgment, prompting the user to succeed or fail. Int I = comm. ExecuteNonQuery (); if (I> 0) {MessageBox. Show ("data added successfully! "," Operation prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information);} else {MessageBox. Show (" adding failed! "," Operation prompt ", MessageBoxButtons. OK, MessageBoxIcon. error);} 3. save private void saveData2 () {dataGridView1.EndEdit (); string SQL = "select * from table name"; OleDbDataAdapter da = new OleDbDataAdapter (SQL, conn) for data changes in the DataGridView ); oleDbCommandBuilder bld = new OleDbCommandBuilder (da); da. updateCommand = bld. getUpdateCommand (); // assign the DataGridView dview to dataTbale. (DataTable) refers to type conversion, provided that the following things can be converted to the able type dataTable dt = (DataTable) dataGridView1.DataSource; da. update (dt); dt. acceptChanges (); conn. close ();}

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.