查询数据库中数据(WinForm+SQL Server)
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;
}
链接:ConDB()方法请参见21.3.1节。
public int Max()
{
SqlCommandcmd = new SqlCommand("select count(*) from t_people", ConDB());
return int.Parse(cmd.ExecuteScalar().ToString());
}
窗体加载事件,通过数据操作类(ClsDBControl)内的Max方法获得总行数用于初使化变量(LastNum)程序代码如下:
private void FrmSelect_Load(object sender, EventArgs e)
{
ClsDB.ClsDBControl CDBLast=new OptDB.ClsDB.ClsDBControl();
LastNum = CDBLast.Max();
}
实现显示“第一条”信息的功能程序代码如下:
private void button1_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();
}
实现显示“上一条”信息的功能程序代码如下:
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("这已是第一条信息");
}
}
实现显示“最后一条”信息的功能程序代码如下:
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
补充:软件开发 , C# ,