program source from: "Visual C # 2008 Program Development and improvement" Min authoring
1. Create a new WinForm project and set the project as the startup project;
2. Copy the database file to the root of the program.
3. Add 3 labels, two textbox, one ListBox, and four button controls.
5. List box named Listboxemployee, four buttons modified by name: Buttonnew, Buttonmdodify, Buttondelete, Buttonexit.
6. Sets the text property of the form, label, text box, button.
Program:
The source code is as follows:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.ComponentModel;UsingSystem.Data;UsingSystem.Drawing;UsingSystem.Text;UsingSystem.Windows.Forms;UsingSystem.Data.OleDb;NamespaceAccess additions and deletions change the source of operation {PublicPartialClassForm1:form {PublicForm1 () {InitializeComponent ();}PrivateOleDbConnection Conn;PrivateOleDbDataAdapter da;PrivateDataSet ds;//The following methods are used to populate the list boxPrivatevoidListboxfill () {if (this.ds.tables[0]. Rows! =Nullthis.ds.tables[0]. Rows.clear ();This.listBoxEmployee.DataSource =Null;This. ListBoxEmployee.Items.Clear ();This.da.Fill (DS,"Employees");This.listBoxEmployee.DataSource = ds. tables["Employees"];This.listBoxEmployee.DisplayMember ="Name";This.listBoxEmployee.ValueMember ="Employee ID"; }//The following event handlers are executed when the form is loadedPrivatevoid Form1_Load (Objectsender, EventArgs e) {String northwindconnectionstring =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=northwind.mdb;";String quuerystring ="Select Employee ID, last name, first name, surname + name as name from employee";This.conn =NewOleDbConnection (NorthwindConnectionString);This.ds =NewDataSet ();THIS.DS.TABLES.ADD ("Employees");This.da =NewOleDbDataAdapter (quuerystring, conn);//Set the Insert command on the data adapter, and the question mark contained in the INSERT statement represents the query parameterThis.da.InsertCommand =New OleDbCommand ("INSERT into employee (surname, first name) VALUES (?,?)", conn);This.da.InsertCommand.Parameters.AddWithValue ("Surname","");This.da.InsertCommand.Parameters.AddWithValue ("Name","");//Sets the update command for the data adapter, and the question mark contained in the UPDATE statement represents the query parameterThis.da.UpdateCommand =New OleDbCommand ("Update employee set Last name =?, First name =? Where employee id=?", conn);This.da.UpdateCommand.Parameters.AddWithValue ("Surname","");This.da.UpdateCommand.Parameters.AddWithValue ("Name","");This.da.UpdateCommand.Parameters.AddWithValue ("Employee ID", -1);//Set the Delete command on the data adapter, and the question mark contained in the DELETE statement represents the query parameterThis.da.DeleteCommand =New OleDbCommand ("Delete * FROM employee where employee id=?", conn);This.da.DeleteCommand.Parameters.AddWithValue ("Id", -1);//Populate a list box with employee name informationThis. Listboxfill (); }//The following event handler is executed when the new button is clickedPrivatevoid Buttonnew_click (Objectsender, EventArgs e) {if (This.textBoxSurName.Text = ="" ||This.textBoxName.Text = ="") {This.labelMsg.Text ="Last name and first name cannot be empty";Return; }//Passing parameters to insert statements by parameter objectsthis.da.insertcommand.parameters["Surname"]. Value =This. Textboxsurname.text;this.da.insertcommand.parameters["Name"]. Value =This. Textboxname.text; DataRow dr =this.ds.tables[0]. NewRow ();//Add a row to the datasheet dr["last name"] = This.textBoxSurName.Text; Set the value for the last Name column of the row dr["First name" = This.textBoxName.Text; Set the value this.ds.tables[0] for the row's name column. Rows.Add (DR); Add the row to the row collection in the datasheet. This.da.Update (This.ds, "employees"); Save the update to a database table. This. Listboxfill (); Populate the list box again This.listBoxEmployee.SelectedIndex = this.listboxemployee.items.count-1; This.labelMsg.Text = "New record added successfully"; This.textBoxSurName.Text = ""; This.textBoxName.Text = ""; This.textBoxSurName.Focus (); }//When you click the Change button, execute the handler private void Buttonmodify_click_1 (object sender, EventArgs e) {int selectedindex = This.listboxemp Loyee. SelectedIndex; Pass the parameter this.da.updatecommand.parameters["employee ID" to the UPDATE statement through the Parameter object. Value = This.listBoxEmployee.SelectedValue; this.da.updatecommand.parameters["Last Name"]. Value = This.textBoxSurName.Text; this.da.updatecommand.parameters["Name"]. Value = This.textBoxName.Text; Gets the currently selected data row of the DataRow dr = this.ds.tables["employee"]. Select ("Employee id=" + This.listBoxEmployee.SelectedValue) [0]; dr["surname"] = This.textBoxSurName.Text; dr["name"] = This.textboxname. Text; This.da.Update (This.ds, "employees"); This. Listboxfill (); This.listBoxEmployee.SelectedIndex = SelectedIndex; This.labelMsg.Text = "The selected record has been updated"; }//When you click the Delete button, execute the following event handler private void Buttondelete_click_1 (object sender, EventArgs e) {//pass parameter T to delete statement by parameter object his.da.deletecommand.parameters["ID"]. Value = This.listBoxEmployee.SelectedValue; Gets the data row to delete DataRow Dr = This.ds.tables[0]. Select ("Employee id=" + This.listBoxEmployee.SelectedValue) [0]; Dr. Delete (); This.da.Update (This.ds, "employees"); This. Listboxfill (); This.labelMsg.Text = "The selected record has been deleted"; }//When you click an item in the list box, execute the following event handler private void Listboxemployee_click (object sender, EventArgs e) {DataRow dr = null; if (this.lis Tboxemployee.selectedindex! =-1) {Dr = This.ds.tables[0]. Select ("Employee id=" + This.listBoxEmployee.SelectedValue) [0]; This.textBoxSurName.Text = dr["Last Name"]. ToString (); This.textBoxName.Text = dr["Name"]. ToString (); }}//When the Click Exit button executes the following event handler private void Buttonexit_click_1 (object sender, EventArgs e) {application.exit ();}}
Project Engineering Source Download:
Link:http://pan.baidu.com/s/1c0d9HN6 Password: QTGB
WinForm Operation Access database additions and deletions learn notes