续(这个例子是针对ACCESS的更新、删除、还有定位的,希望能对你
来源:岁月联盟
时间:2003-07-12
//
this.btn_help.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.btn_help.Location = new System.Drawing.Point(360, 280);
this.btn_help.Name = "btn_help";
this.btn_help.Size = new System.Drawing.Size(88, 24);
this.btn_help.TabIndex = 3;
this.btn_help.Text = "帮 助(&h)";
this.btn_help.Click += new System.EventHandler(this.btn_help_Click);
//
// label5
//
this.label5.BackColor = System.Drawing.Color.Olive;
this.label5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label5.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label5.Location = new System.Drawing.Point(48, 192);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(152, 24);
this.label5.TabIndex = 1;
this.label5.Text = "市场价格:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txt_bookstock
//
this.txt_bookstock.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txt_bookstock.Location = new System.Drawing.Point(232, 240);
this.txt_bookstock.Name = "txt_bookstock";
this.txt_bookstock.Size = new System.Drawing.Size(216, 21);
this.txt_bookstock.TabIndex = 2;
this.txt_bookstock.Text = "";
//
// dataedit
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.Olive;
this.ClientSize = new System.Drawing.Size(536, 389);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar,
this.btn_help,
this.txt_BookId,
this.label2,
this.label1,
this.label3,
this.label4,
this.label5,
this.label6,
this.txt_BookName,
this.txt_AuthorName,
this.txt_bookprice,
this.txt_bookstock,
this.btn_Delete,
this.btn_update,
this.btn_movenext,
this.btn_movefirst,
this.btn_moveprevious,
this.btn_movelast});
this.Name = "dataedit";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "dataedit";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
private void MovePrevious()
{
if (myBind.Position == 0)
{
statusBar.Text = "数据已经到头";
}
else
{
myBind.Position -= 1;
statusBar.Text = "当前位置:" +myBind.Position.ToString();
}
}
private void MoveFirst()
{
myBind.Position = 0;
statusBar.Text = "当前位置:" +myBind.Position.ToString();
}
private void MoveNext()
{
if (myBind.Position == myBind.Count-1)
{
statusBar.Text = "数据已经到尾";
}
else
{
myBind.Position += 1;
statusBar.Text = "当前位置:" +myBind.Position.ToString();
}
}
private void MoveLast()
{
myBind.Position = myBind.Count-1;
statusBar.Text = "当前位置:" +myBind.Position.ToString();
}
private void btn_movefirst_Click(object sender, System.EventArgs e)
{
MoveFirst();
}
private void btn_moveprevious_Click(object sender, System.EventArgs e)
{
MovePrevious();
}
private void btn_movenext_Click(object sender, System.EventArgs e)
{
MoveNext();
}
private void btn_movelast_Click(object sender, System.EventArgs e)
{
MoveLast();
}
private void btn_help_Click(object sender, System.EventArgs e)
{
MessageBox.Show("数据库操作练习——定位、更新、删除",System.DateTime.Now.ToShortDateString());
}
private void btn_Delete_Click(object sender, System.EventArgs e)
{
try
{
string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=book.mdb";
OleDbConnection conn = new OleDbConnection(strconn);
conn.Open();
string strdelete = "select * from bookstock";
OleDbDataAdapter adapter = new OleDbDataAdapter(strdelete,conn);
myDataSet.Tables["bookstock"].Rows[myBind.Position].Delete();
adapter.Update(myDataSet,"bookstock");
statusBar.Text = "记录已经删除";
conn.Close();
}
catch(Exception ed)
{
statusBar.Text = "发生错误,原因:" + ed.Message;
MessageBox.Show(ed.ToString());
}
}
private void btn_update_Click(object sender, System.EventArgs e)
{
try
{
string strconn = "Provider=Microsoft.Jet.OlEDB.4.0;Data Source=book.mdb";
OleDbConnection conn = new OleDbConnection(strconn);
conn.Open();
string strupdt = "update bookstock set booktitle = '"
+ txt_BookName.Text + "',bookauthor = '"
+ txt_AuthorName.Text + "',bookprice = "
+ txt_bookprice.Text +",bookstock = "
+ txt_bookstock.Text +" Where bookid = " + txt_BookId.Text;
OleDbCommand comm = new OleDbCommand(strupdt,conn);
comm.ExecuteNonQuery();
statusBar.Text = "更新成功!";
conn.Close();
myDataSet = null;
myBind = null;
if(isbound)
{
txt_BookId.DataBindings.Clear();
txt_BookName.DataBindings.Clear();
txt_AuthorName.DataBindings.Clear();
txt_bookprice.DataBindings.Clear();
txt_bookstock.DataBindings.Clear();
isbound = false;
}
GetConnected();
}
catch(Exception eu)
{
statusBar.Text = "发生错误,原因:" + eu.Message;
}
}
}
}