Easily browse database records in Visual C #

xiaoxiao2021-03-06  22

Programming with Delphi or VB, when performing records in the database, often uses components named data navigator, through this component, can be very convenient to implement data tables that have been bound to this component Record browsing. The so-called last record, next record, first record, tail record, etc. So do you have such components in Visual C #? the answer is negative. But because Visual C # has a powerful database processing capability, it is more convenient to make a program similar to this component. This article is to introduce the specific production process of this program. First, the program's main function introduction: Program opens the Book data table in the local Acess database (Sample.mdb), and then binds the fields in the Book data table to the text box provided by the program, displayed. Implement the record browsing of the Book data table through the four buttons of the program "First Record", "Tail Record", "Previous", "Next". The run interface of the program is as follows: Figure 01: Environmental settings for the run interface of the browsing program in the data table: (1) Window 2000 Server Edition (2) Microsoft Acess Data Component 2.6 (MADC 2.6) Three, Problem for programming difficulties and should pay attention to: (1) How to implement the fields in the data table to display: If you directly assign the value to the text box directly, if you use the "Next" button to browse the data When the record is recorded, the value of the text box will not change. How to make the text box dynamically displays the value field value according to the record pointer in the data table, which is a key point in this article and a difficult point. This article implements dynamically display field value by binding the field values ​​in the data table to the "text" property of the text box. Implement this processing to use the DataBindings property of the text box and the Add method. The specific syntax is as follows: Text component name .DATABINDINGS.ADD ("text", DataSet object, data table and field name); in the program specific as follows: t_bookid.databindings.add ("text", mydataset, "books.bookid"); This allows the field value to display according to the record pointer. (2) How to change the record pointer: Only master how to change the record pointer, you can record the records you want. Visual C # Changing the record pointer is implemented by a named BindingManagerBase object. This object is packaged in the namespace system.windows.froms. BindingManagerBase object is an abstract object that manages all binding similar data sources and data members. In programming, the two properties in the BindingManagerBase object are mainly used in programming, namely: Position attributes and count properties. The first attribute is the current pointer to the data set, and the latter attribute is the total number of records in the current data set.

This allows for program code corresponding to the four buttons of the record pointer: i>. First record: mybind.position = 0; II>. Take record: mybind.position = mybind.count - 1; iii>. Next record And post-operation run interface: if (mybind.position == mybind.count -1) messagebox.show ("has reached the last record!"); Else mybind.position = 1; iv>. Previous record and operation Run interface: if (mybind.position == 0) MessageBox.show ("has reached the first record!"); Else mybind.position - = 1;

Four source code: using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Data.OleDb; using System.Data; public class DataView: Form {private System.ComponentModel.Container components; private Button lastrec; private Button nextrec; private Button previousrec; private Button firstrec; private TextBox t_books; private TextBox t_bookprice; private TextBox t_bookauthor; private TextBox t_booktitle; private TextBox t_bookid; private Label l_books; private Label l_bookprice; private Label l_bookauthor; private Label l_booktitle; private Label l_bookid; private Label label1; private System.Data.DataSet myDataSet; private BindingManagerBase myBind; public DataView () {// connect to a database getConnected (); // needed for the content of the form is InitializeComponent (); (); computer.dispose ();} public static void main ();} public static void main ();} public void getConnected (); t ry {// create a OleDbConnection string strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = sample.mdb"; OleDbConnection myConn = new OleDbConnection (strCon); string strCom = "SELECT * FROM books"; // Create a Dataset MyDataSet = New DataSet (); myconn.open (); // Get a data set OLEDBDataAdapter myCommand = new oledbdataadapter (STRCOM, MyConn); // Bind DataSet Books Datutical MyCommand.Fill (MyDataSet, "Books"); // Close this oledbconnection myconn.close ();} catch (exception e) {messagebox.show ("Connection Error!" E.TOString (), "Error");}} private void InitializationComponent ) {this.components = new system.componentmodel.container ();

THIS.T_BOOKID = New TEXTBOX (); this.nexTrec = new button (); this.lastrec = new button (); this.l_bookid = new label (); this.t_books = new textbox (); this.t_booktitle = new TEXTBOX (); this.t_bookprice = new textBox (); this.firstrec = new button (); this.l_booktitle = new label (); this.l_bookprice = new label (); this.l_books = new label (); this .previousrec = new button (); this.l_bookauthor = new label (); this.t_bookauthor = new textbox (); this.label1 = new label (); // The following is to initialize the four buttons for data. FirstRec. Location = new System.Drawing.Point (55, 312); firstrec.ForeColor = System.Drawing.Color.Black; firstrec.Size = new System.Drawing.Size (40, 24); firstrec.TabIndex = 5; firstrec. Font = new system.drawing.font ("imitation Song", 8F); firstRec.Text = "First Record"; firstRec.click = new system.EventHandler (gofirst); PreviousRec.Location = new system.drawing.point (125 , 312); PreviousRec.Forecolor = system.drawing.color.black; previousRec.size = new system.drawin G.Size (40, 24); previousRec.Tabindex = 6; previousrec.font = new system.drawing.font ("imitation Song", 8F); previousRec.Text = "Previous"; PreviousRec.click = New System. EventHandler (GoPrevious); nextrec.Location = new System.Drawing.Point (195, 312); nextrec.ForeColor = System.Drawing.Color.Black; nextrec.Size = new System.Drawing.Size (40, 24); nextrec . TabINDEX = 7; NextRec.font = new system.drawing.font ("imitation Song", 8F); NextRec.Text = "Next"; Next "; NextRec.Click = new system.eventhandler (gonext); LastRec.location =

New system.drawing.point (265, 312); LastRec.ForeColor = system.drawing.color.black; lastrec.size = new system.drawing.size (40, 24); LastRec.tabindex = 8; LastRec.Font = New system.drawing.font ("imitation Song", 8F); LastRec.Text = "Tail Record"; LastRec.Click = New System.EventHandler (GOLAST); // The following is a label set to display data record And text box for initialization, and bind records to different bound to text box "text" attribute on t_bookid.location = new system.drawing.point (184, 56); t_bookid.tabindex = 9; t_bookid.size = New system.drawing.size (80, 20); t_bookid.databindings.add ("text", mydataset, "books.bookid"); t_books.location = new system.drawing.point (184, 264); t_books.tabindex = 10; t_books.size = new system.drawing.size (80, 20); t_books.databindings.add ("text", mydataset, "books.bookstock"); t_booktitle.location = new system.drawing.point (184 , 108); t_booktitle.tabindex = 11; t_booktitle.size = new system.drawing.size (176, 20); t_booktitle.databindings.add ("text", mydataset, "books.booktitle"); t_bookprice .Location = new system.drawing.point (184, 212); t_bookprice.tabindex = 12; t_bookprice.size = new system.drawing.size (80, 20); t_bookprice.databindings.add ("text", mydataset, " books.bookprice "); t_bookauthor.Location = new System.Drawing.Point (184, 160); t_bookauthor.TabIndex = 18; t_bookauthor.Size = new System.Drawing.Size (128, 20); t_bookauthor.DataBindings.Add ( "Text", mydataset, "books.bookauthor"); l_bookid.location = new system.drawing.point (24, 56); l_bookid.text = book number: ";

l_bookid.Size = new System.Drawing.Size (112, 20); l_bookid.Font = new System.Drawing.Font ( "italics", 10f); l_bookid.TabIndex = 13; l_bookid.TextAlign = System.Drawing.ContentAlignment. MiddleCenter; l_booktitle.location = new system.drawing.point (24, 108); l_booktitle.text = "Title:"; l_booktitle.size = new system.drawing.size (112, 20); l_booktitle.font = new system .Drawing.Font ( "italics", 10f); l_booktitle.TabIndex = 14; l_booktitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; l_bookprice.Location = new System.Drawing.Point (24, 212); l_bookprice.Text = "Price:"; l_bookprice.size = new system.drawing.size (112, 20); l_bookprice.font = new system.drawing.Font ("imitation Song", 10F); l_bookprice.TabINDEX = 15; l_bookprice.textalign = system .Drawing.contentalignment.middleCenter; l_books.location = new system.drawing.point (24, 264); l_books.text = bookshelf number: "; l_books.size = new system.drawing.size (112, 20); l_books .Font = new system.drawing.font (" Italics ", 10f); l_books.TabIndex = 16; l_books.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; l_bookauthor.Location = new System.Drawing.Point (24, 160); l_bookauthor.Text =" Analysis: "; l_bookauthor .Size = new System.Drawing.Size (112, 20); l_bookauthor.Font = new System.Drawing.Font ( "italics", 10f); l_bookauthor.TabIndex = 17; l_bookauthor.TextAlign = System.Drawing.ContentAlignment.MiddleCenter Label1.Location = new system.drawing.point (49, 8); label1.text = "Browse Book Information"; Label1.Size =

New system.drawing.size (296, 24); label1.forecolor = system.drawing.color.d; label1.font = new system.drawing.font ("imitation Song", 15F); label1.tabindex = 19; // Set this.Text = "Use C # to make the program recorded in the database!"; This.autoscalebasesize = new system.drawing.size (5, 13); this.formolderstyle = formborderstyle.fixedsingle; this.clientsize = New system.drawing.size (394, 375); // Add component this.controls.add (lastrec); this.controls.add (PreviousRec); this.controls.add (PreviousRec); this. Controls.Add (firstrec); this.Controls.Add (t_books); this.Controls.Add (t_bookprice); this.Controls.Add (t_bookauthor); this.Controls.Add (t_booktitle); this.Controls.Add (t_bookid ); this.controls.add (l_books); this.controls.add (l_bookprice); this.controls.add (l_bookauthor); this.controls.add (l_booktitle); this.controls.add (l_bookid); this.controls .Add (label1); // Bind the object Dataset and "Books" data sheet to this mybind object mybind = this.bindingContext [myDataSet, "Books"];} // button " Take record "Object" Object "Object Sender, System.EventArgs E) {mybind.position = mybind.count - 1;} // button" Next "Object Event Protected Void Gonext (Object Sender, System.EventArgs e) {if (mybind.position == mybind.count -1) MessageBox.show ("has reached the last record! "); else mybind.position = 1;} // button" Previous "Object Event Program protected void goprevious (Object sender, system.eventargs e) {if (mybind.position == 0) MessageBox.show (" already Go to the first record! "); Else mybind.position - = 1;} // button" first record "object event program protected void gofirst (Object sender, system.eventargs e) {mybind.position = 0;

转载请注明原文地址:https://www.9cbs.com/read-42963.html

New Post(0)