Query database data (WinForm + SQL Server)

Source: Internet
Author: User

 

Input a select statement for the SqlDataAdapter class, add the result to the DataSet result set in the form of rows through the Fill method, and then present the result to the user in the form of scroll below. The main program code is as follows. The Fill method of the SqlDataAdapter class is used to extract a piece of data from the query result and add it to the dataSet result set.

PublicDataSetdsResult (int currentIndex)
{
Int pageSize = 1;
String orderSQL = "SELECT * FROM t_People order by tb_PID ";
SqlDataAdapter adapter = new SqlDataAdapter (orderSQL, ConDB ());
DataSetdataSet = newDataSet ("t_People ");
Adapter. Fill (dataSet, currentIndex, pageSize, "t_People ");
ReturnDataSet;
} Link: For the ConDB () method, see section 21.3.1.

Obtain the total number of records in the table, which is used to identify the index value of the last information.

Public int Max ()
{
SqlCommandcmd = new SqlCommand ("select count (*) from t_people", ConDB ());
Return int. Parse (cmd. ExecuteScalar (). ToString ());
} Form loading event. Obtain the total number of rows through the Max method in the ClsDBControl class. The code for the LastNum program is as follows:

Private void FrmSelect_Load (object sender, EventArgs e)
{
ClsDB. ClsDBControl CDBLast = new OptDB. ClsDB. ClsDBControl ();
LastNum = CDBLast. Max ();
} The program code for displaying the "first" information is as follows:

Private void button#click (object sender, EventArgs e)
{
I = 0;
ClsDB. ClsDBControl cdb = new OptDB. ClsDB. ClsDBControl ();
DataSetdsNew = cdb. dsResult (I );
This. textBox1.Text = dsNew. Tables [0]. Rows [0] [0]. ToString ();
This. textBox2.Text = dsNew. Tables [0]. Rows [0] [1]. ToString ();
This. textBox3.Text = dsNew. Tables [0]. Rows [0] [2]. ToString ();
} The program code for displaying "previous" information is as follows:

Private void button2_Click (object sender, EventArgs e)
{
I-= 1;
If (I> = 0)
{
ClsDB. ClsDBControl cdb = new OptDB. ClsDB. ClsDBControl ();
DataSetdsNew = cdb. dsResult (I );
This. textBox1.Text = dsNew. Tables [0]. Rows [0] [0]. ToString ();
This. textBox2.Text = dsNew. Tables [0]. Rows [0] [1]. ToString ();
This. textBox3.Text = dsNew. Tables [0]. Rows [0] [2]. ToString ();
}
Else
{
I + = 1;
MessageBox. Show ("This is the first message ");
}
} The program code for displaying the "last" information is as follows:

Private void button4_Click (object sender, EventArgs e)
{
ClsDB. ClsDBControl cdb = new OptDB. ClsDB. ClsDBControl ();
DataSetdsNew = cdb. dsResult (LastNum-1 );
This. textBox1.Text = dsNew. Tables [0]. Rows [0] [0]. ToString ();
This. textBox2.Text = dsNew. Tables [0]. Rows [0] [1]. ToString ();
This. textBox3.Text = dsNew. Tables [0]. Rows [0] [2]. ToString ();
& Nbs

Related Article

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.