Visual C # easy to browse database records in Visual C #

xiaoxiao2021-03-06  39

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 main features of the program:

The program opens the Book data table in the local acess database (Sample.mdb), then put the book data table

Field Binds 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: Running interface of the browsing program in the data sheet

Second, the program design and running environment settings:

(1) Window 2000 server version

(2) Microsoft Acess Data Component 2.6 (MADC 2.6)

Third, the program is difficult to design and should pay attention to:

(1) How to implement the fields in the data table to display:

If you directly assign a value to the text box directly, if you use the "Next" button to browse the data record, 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 specific procedure 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 how to change the record pointer to browse the record as 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 can be obtained by changing the program code corresponding to the four buttons of the record pointer:

I>. First Record: mybind.position = 0; II>. Take: mybind.position = mybind.count - 1; iii>. The next record and operation After running interface: if (mybind.position == mybind.count -1) Messagebox.show ("has reached the last record!"); ElseMybind.position = 1; IV>. The last record and post-run operation interface: if (mybind.position == 0) MessageBox.show (" Already reached the first record! "); Elsemybind.position - = 1; 4. 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 (); // InitializeComponent (), INITIALIZECMMPONENT ();} Public override void dispose () {base.dispose (); Components.dispose ();} public static void main () {Application.run ());} public void getConnected () {TRY {// Create a OLEDBConnectionstring strcon = "provide R = microsoft.jet.Oledb.4.0; data source = sample.mdb "; OLEDBConnection myconn = new oledbconnection (strcon); string strcom =" select * from books "; // Create a DataSetMyDataSet = new dataset (); MyConn. Open (); // get a dataset OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom, myConn) with OleDbDataAdapter; // bind the books dataset data sheet myCommand.Fill (myDataSet, "books"); // Close OleDbConnectionmyConn.Close (); "EXCEPTION E) {MessageBox.show (" Connection Error! " E.TOString ()," Error ");}} private void initializeComponent () {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 of the data for initialization 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 = "recording head"; firstrec.Click = new System.EventHandler (GoFirst); previousrec.Location = new System.Drawing.Point (125, 312); previousrec.ForeColor = System.Drawing.Color.Black; previousrec. Size = new system.drawing.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 System.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 (//) is initialized for the tag and text box set to display data record, and binds the record binding in different binding to text box "text" attributes 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 ("imitation Song", 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 =" shelf 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 = ne w 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.green; label1.font = new system.drawing.font ("imitation Song", 15F);

Label1.tabindex = 19; // Set this.text = "Use C # to browse the program recorded in the database!"; this.autoscalebasesize = new system.drawing.size (5, 13); this.FormBorderstyle = Formborderstyle.fixedsingLe; this.clientsize = new system.drawing.size (394, 375); // Add component this.controls.add (LastRec) in the form; this.controls.add (next.controls); 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 Record "Object" protected "Object sender, system.eventargs e) {mybind.position = mybind.count - 1;} // button" Next "Object Event Protected Void Gonext (Object Sender, SYST EM.Eventargs e) {if (mybind.position == mybind.count -1) MessageBox.show ("has arrived at the last record! "); elseMybind.position = 1;} // button" Previous "object event program protected void goprevious (Object sender, system.eventargs e) {if (mybind.position == 0) MessageBox.show (" has arrived The first record! "); Elsemybind.position - = 1;} // button" First Record "Object Event Program Protected Void Gofirst (Object Sender, System.EventArgs E) {mybind.position = 0;}} 5. Summary :

The focus of this paper is how to change the record pointer of the Dataset with Visual C # and how to change the text box according to the change of the record pointer. Although this type of treatment is more troublesome than in Visual C #. However, the programming staff is more flexible, making program designers have a greater development space.

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

New Post(0)