Easy browsing of database records in Visual C # (Turn)

Source: Internet
Author: User
Tags bind count implement
visual| Data | Database with Delphi or VB programming, in the database to operate the records, often used a name called the Data Navigator component, through this component, can be very convenient for the implementation of the data table has been bound to this component to browse the records. is called the previous record, the next record, the first record, the tail record and so on. Is there such a component in Visual C #? The answer is in the negative. However, because Visual C # has powerful database processing capabilities, it is easier to make a program similar to this component. This article is to introduce the procedure of the specific production process.

First, the main functions of the program introduction:
The program opens the Book data table in the local acess database (Sample.mdb) and then puts the
The field is bound to the text box provided by the program and is displayed. The four buttons in the program, "first record," "Tail record," "Previous", "next", to achieve the book data table browsing. The running interface of the program is as follows:

Figure 01: Running interface to the record viewer in a datasheet

Second, the program design and operation of the environment settings:
(1) Windows 2000 Server Edition
(2) Microsoft acess Data Component 2.6 (MADC 2.6)

Third, the programming difficulty and should pay attention to the question:
(1) How to implement the data table in a text box to display the field:
If you assign the value of a field directly to a text box, the value of the text box will not change if you use the Next button to navigate through the data record. How to make a text box dynamically display the value of a field based on the record pointer in the datasheet is an important and difficult part of this article.
This article implements the dynamic display of field values by binding the value of the field in the datasheet to the Text property of the TextBox. Implement this process to use the DataBindings property of the text box and the Add method therein. The specific syntax is as follows:
The name of the text component. 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 you to implement the field values to display based on the record pointer.
(2) How to change the record pointer:
Only master how to change the record pointer, you can browse the record. Visual C # Changes the record pointer through a life called BindingManagerBase object. This object is encapsulated in the namespace System.Windows.Froms. The BindingManagerBase object is an abstract object that manages all bound homogeneous data sources and data members. In programming, the main use of two attributes in the BindingManagerBase object, namely: the Position property and the Count property. The first property is a record of the current pointer to the dataset, and the last property is the total number of records in the current dataset. This allows you to get the program code for the four buttons that change the record pointer:
I> first record:
mybind.position = 0;
ii> tail Record:
Mybind.position = mybind.count-1;
III> Run the interface after the next record and operation:
if (mybind.position = = mybind.count-1)
MessageBox.Show ("It's the last record!") " ) ;
Else
Mybind.position + 1;

IV> Run the interface after the previous record and operation:
if (mybind.position = 0)
MessageBox.Show ("It's the first record!") " ) ;
Else
Mybind.position-= 1;



Four. Program 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 ();
Initialize the content that is required in the form
InitializeComponent ();
}
public override void Dispose () {
Base. Dispose ();
Components. Dispose ();
}
public static void Main () {
Application.Run (New DataView ());
}
public void getconnected ()
{
try{
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 ();
Using OleDbDataAdapter to get a dataset
OleDbDataAdapter mycommand = new OleDbDataAdapter (strcom, myconn);
Bind a dataset to books data tables
Mycommand.fill (myDataSet, "books");
Close this OleDbConnection
Myconn.close ();
}
catch (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 an initialization of the four buttons for data browsing
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", 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.Drawing.Size (40, 24);
Previousrec. TabIndex = 6;
Previousrec. Font = new System.Drawing.Font ("imitation", 8f);
Previousrec. Text = "previous One";
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", 8f);
Nextrec. Text = "Next Bar";
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", 8f);
Lastrec. Text = "tail record";
Lastrec. Click + + new System.EventHandler (golast);
The following is an initialization of the labels and text boxes that are set up to display data records and bind the records to a different bound to the text box "text" Property
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 Serial number:";
L_bookid. Size = new System.Drawing.Size (112, 20);
L_bookid. Font = new System.Drawing.Font ("imitation", 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 ("imitation", 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", 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 ("imitation", 10f);
L_books. TabIndex = 16;
L_books. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

L_bookauthor. Location = new System.Drawing.Point (24, 160);
L_bookauthor. Text = "Author:";
L_bookauthor. Size = new System.Drawing.Size (112, 20);
L_bookauthor. Font = new System.Drawing.Font ("imitation", 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", 15f);
Label1. TabIndex = 19;
To set up a form
This. Text = "Use C # to do the browsing database in the program!" ";
This. AutoScaleBaseSize = new System.Drawing.Size (5, 13);
This. FormBorderStyle = FormBorderStyle.FixedSingle;
This. ClientSize = new System.Drawing.Size (394, 375);
Add a component to a form
This. Controls.Add (LASTREC);
This. Controls.Add (NEXTREC);
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 the Books data table to this Mybind object
Mybind= this. BindingContext [myDataSet, "books"];
}
Button "Tail Record" object event program
protected void Golast (object sender, System.EventArgs e)
{
Mybind.position = mybind.count-1;
}

Button "Next" Object event program
protected void GoNext (object sender, System.EventArgs e)
{
if (mybind.position = = mybind.count-1)
MessageBox.Show ("It's 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 ("It's the first record!") " ) ;
Else
Mybind.position-= 1;
}
Button "First record" object event program
protected void Gofirst (object sender, System.EventArgs e)
{
mybind.position = 0;
}
}


Five. Summary:
The focus of this article is how to use Visual C # to change the data set's record pointer and how to change the display of the text box according to the record pointer. Although this type of processing is more cumbersome in Visual C # than in other languages. But for the program designers are more flexible, so that the program designers have a greater space for development.


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.