首页上一页 1 下一页尾页 1 条记录 1/1页
《C#程序开发范例宝典》数据库技术章节疑问
发表在C#图书答疑
2012-04-24
是否精华
是
否
版块置顶:
是
否
《C#程序开发范例宝典》第九章实例296存在问题:操作获取最后一条数据跟第一条数据是无问题的。可是如果点击下一条记录按钮,紧接着点击上一条记录按钮,程序无反应,需连续点击两次才有效果。
附上更改后的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.OleDb;
namespace Access2000Path
{
public partial class Form1 : Form
{
OleDbConnection Olecon;
OleDbDataAdapter OleDat;
DataTable dt ;
int MaxValue = 0,State = 0;
string ConStr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strPath = Application.StartupPath + "\\db_09.mdb";
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strPath;
Olecon = new OleDbConnection(ConStr);
Olecon.Open();
MaxValue =Convert.ToInt32( new OleDbCommand("select Count(*) from 帐目", Olecon).ExecuteScalar());
Olecon.Close();
showInfo(0, 1);
}
private void showInfo(int first,int next)
{
dt = new DataTable();
OleDat = new OleDbDataAdapter("select * from 帐目", ConStr);
OleDat.Fill(first, next, dt);
this.textBox1.Text = dt.Rows[0][1].ToString();
this.textBox2.Text = dt.Rows[0][2].ToString();
this.textBox3.Text = dt.Rows[0][4].ToString();
this.textBox4.Text = dt.Rows[0][5].ToString();
}
//第一条
private void button1_Click(object sender, EventArgs e)
{
showInfo(0, 1);
State = 0;
}
//最后一条
private void button4_Click(object sender, EventArgs e)
{
showInfo(MaxValue-1, MaxValue );
State = MaxValue-1;
}
//下一条
private void button3_Click(object sender, EventArgs e)
{
if (State < MaxValue -1)
{
++State;
showInfo(State, State + 1);
}
else
{
showInfo(0, 1);
State = 0;
}
}
//上一条
private void button2_Click(object sender, EventArgs e)
{
if (State > 0)
{
--State;
showInfo(State, State + 1);
}
else
{
showInfo(MaxValue - 1, MaxValue);
State = MaxValue - 1;
}
}
}
}
附上更改后的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.OleDb;
namespace Access2000Path
{
public partial class Form1 : Form
{
OleDbConnection Olecon;
OleDbDataAdapter OleDat;
DataTable dt ;
int MaxValue = 0,State = 0;
string ConStr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string strPath = Application.StartupPath + "\\db_09.mdb";
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + strPath;
Olecon = new OleDbConnection(ConStr);
Olecon.Open();
MaxValue =Convert.ToInt32( new OleDbCommand("select Count(*) from 帐目", Olecon).ExecuteScalar());
Olecon.Close();
showInfo(0, 1);
}
private void showInfo(int first,int next)
{
dt = new DataTable();
OleDat = new OleDbDataAdapter("select * from 帐目", ConStr);
OleDat.Fill(first, next, dt);
this.textBox1.Text = dt.Rows[0][1].ToString();
this.textBox2.Text = dt.Rows[0][2].ToString();
this.textBox3.Text = dt.Rows[0][4].ToString();
this.textBox4.Text = dt.Rows[0][5].ToString();
}
//第一条
private void button1_Click(object sender, EventArgs e)
{
showInfo(0, 1);
State = 0;
}
//最后一条
private void button4_Click(object sender, EventArgs e)
{
showInfo(MaxValue-1, MaxValue );
State = MaxValue-1;
}
//下一条
private void button3_Click(object sender, EventArgs e)
{
if (State < MaxValue -1)
{
++State;
showInfo(State, State + 1);
}
else
{
showInfo(0, 1);
State = 0;
}
}
//上一条
private void button2_Click(object sender, EventArgs e)
{
if (State > 0)
{
--State;
showInfo(State, State + 1);
}
else
{
showInfo(MaxValue - 1, MaxValue);
State = MaxValue - 1;
}
}
}
}