Visual C#中轻松浏览数据库记录(转)
http://www.downtool.com/tech/1135.html
这是以前版本的版本,现在已经有很多问题了。不太好用了。呵。经过了自己的修改,终于跑起来了。
fill DataSet 再绑定到控件
private System.Data.DataSet myDataSet;
private BindingManagerBase myBind;
public ResumeBrowse()
{
GetConnected();
//把对象DataSet和"books"数据表绑定到此myBind对象
myBind = this.BindingContext[myDataSet, "MResume"];
InitializeComponent();
}
public static OleDbConnection GetConnection()
{
OleDbConnection conn = null;
string strconnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "database//chinabase.mdb;Persist Security Info=True";
try
{
conn = new OleDbConnection(strconnectionString);
}
catch (Exception ex)
{
throw ex;
}
return conn;
}
public void GetConnected()
{
try{
//创建一个 OleDbConnection
OleDbConnection myConn = GetConnection() ;
string strCom = "SELECT * FROM MResume order by id desc" ;
//创建一个 DataSet
myDataSet = new DataSet () ;
myConn.Open () ;
//用 OleDbDataAdapter 得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter (strCom,myConn) ;
//把Dataset绑定books数据表
myCommand.Fill (myDataSet,"MResume");
//关闭此OleDbConnection
myConn.Close () ;
}
catch ( Exception e )
{
MessageBox.Show ( "连接错误! " + e.ToString(),"错误");
}
}
//按钮"尾记录"对象事件程序
protected void GoLast(object sender , System.EventArgs e)
{
myBind.Position = myBind.Count - 1 ;
//ResumeBrowse_Load(sender,e); //窗体加载
}
//按钮"下一条"对象事件程序
protected void GoNext (object sender , System.EventArgs e)
{
if (myBind.Position == myBind.Count -1)
MessageBox.Show ("已经到了最后一条记录!","提示") ;
else
myBind.Position += 1 ;
}
//按钮"上一条"对象事件程序
protected void GoPrevious (object sender , System.EventArgs e)
{
if (myBind.Position == 0)
MessageBox.Show ("已经到了第一条记录!","提示") ;
else
myBind.Position -= 1 ;
}
//按钮"首记录"对象事件程序
protected void GoFirst (object sender , System.EventArgs e)
{
myBind.Position = 0 ;
}
private void ResumeBrowse_Load(object sender, EventArgs e)
{
//t_bookid.DataBindings.Add("Text", myDataSet, "MResume.姓名");
//t_bookprice.DataBindings.Add("Text", myDataSet, "MResume.性别");
//t_bookauthor.DataBindings.Add("Text", myDataSet, "MResume.出生日期");
lblname.DataBindings.Add("Text", myDataSet, "MResume.姓名");
lblsex.DataBindings.Add("Text", myDataSet, "MResume.性别");
lblbirth.DataBindings.Add("Text", myDataSet, "MResume.出生日期");
lblyear.DataBindings.Add("Text", myDataSet, "MResume.工作年限");
lblcardstyle.DataBindings.Add("Text", myDataSet, "MResume.证件类型");
lblcardnum.DataBindings.Add("Text", myDataSet, "MResume.证件号");
lbladdress.DataBindings.Add("Text", myDataSet, "MResume.居住地");
lblemail.DataBindings.Add("Text", myDataSet, "MResume.Email");
lblphone.DataBindings.Add("Text", myDataSet, "MResume.手机号码");
lblhomephone.DataBindings.Add("Text", myDataSet, "MResume.家庭电话");
lblpic.DataBindings.Add("Text", myDataSet, "MResume.图片");
lbldetail.DataBindings.Add("Text", myDataSet, "MResume.自我评价");
}