DataGridView the Access database by adding and pruning

Source: Internet
Author: User
Tags name database

Preparatory work:

Create a new WinForm project with Visual Studio, put a DataGridView control in the Form1, and two button controls.

Create a new Access database under the folder directory bin/debug/the project, with the name database. It is important to note that a. mdb (Access 2002-2003 database) is preferable. If you use. ACCDB (access2007 format, you need to download and install access to the runtime, because version 07 uses the provider engine different from the original, and Office2007 does not bring this. Create a new table in database, name Tblmat, add the primary key field "ID", type automatically increment, other fields are arbitrary.

Write code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSystem.Data.OleDb; One  A namespacedatagridviewtest - { -      Public Partial classForm1:form the     { -         //a connection string used to connect database databases; -         //If there is no password, remove the Jet oledb:database password=***; -          Public Static stringConnString =@" + provider=microsoft.jet.oledb.4.0; - Data Source=database.mdb; + JET oledb:database password=***; A         "; at         //SQL query statement to get all the data from the Database Tblmat table; -         Private stringSqlString ="SELECT * from Tblmat"; -         //DataAdapter, making database tables and in-memory table DataTable communication -         PrivateOleDbDataAdapter da; -         //BindingSource, making the In-memory table DataTable communicate with the form's display control DataGridView -         PrivateBindingSource BS; in          PublicForm1 () -         { to InitializeComponent (); +             //New Connection -OleDbConnection conn =NewOleDbConnection (connstring); the             //New DataAdapter *Da =NewOleDbDataAdapter (sqlString, conn); $             //New DataTablePanax NotoginsengDataTable dt =NewDataTable (); -             //If the data adapter fills the memory table without setting the primary key column, and the Access data table has, then set the primary key; the             //if the primary key of an Access data table is automatically incremented, the primary key of the Set memory table is also automatically incremented. +Da. MissingSchemaAction =MissingSchemaAction.AddWithKey; A             //Populating the Memory table the da. Fill (DT); +             //If the MissingSchemaAction is set, the following statement can be omitted; -             //dt. PrimaryKey = new datacolumn[] {dt. Columns[0]}; $             //dt. Columns[0]. AutoIncrement = true; $             //dt. Columns[0]. AutoIncrementSeed = 1; -             //dt. Columns[0]. AutoIncrementStep = 1; -  the             //New BindingSource -BS =NewBindingSource ();Wuyi             //BindingSource binding Memory Table theBs. DataSource =DT; -             //DataGridView Binding BindingSource WuDatagridview1.datasource =BS; -         } About  $         //update the database, including additions and deletions, if you need to update in real-time, and you want the client and database communication is not too frequent, -         //set the following code content in the DataGridView Rowleave event -         Private voidButton1_Click (Objectsender, EventArgs e) -         { A             //automatically generate additional SQL statements from the data adapter's Select command text statement +             //include Update, Insert, Delete theOleDbCommandBuilder cb =NewOleDbCommandBuilder (DA); -             //The following statements have been tested without $             //da. UpdateCommand = cb. GetUpdateCommand (); the             //da. InsertCommand = cb. GetInsertCommand (); the             //da. DeleteCommand = cb. GetDeleteCommand (); the             //Datagridview1.endedit (); the             //Conn.Open (); -  in             //Update an Access database automatically with the DataAdapter Update method the da. Update (DataTable) BS. DataSource); the             //conn.close (); About         } the  the         //Locate the second cell of the new row in DataGridView, the         //the first cell format "ID", automatically incremented. +         Private voidButton2_Click (Objectsender, EventArgs e) -         { theDatagridview1.currentcell = Datagridview1.rows[datagridview1.newrowindex]. cells[1];Bayi         } the     } the}

DataGridView the Access database by adding and pruning

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.