📄 form1.cs
字号:
this.BtEdit.Click += new System.EventHandler(this.BtEdit_Click);
//
// BtDel
//
this.BtDel.Location = new System.Drawing.Point(8, 64);
this.BtDel.Name = "BtDel";
this.BtDel.TabIndex = 1;
this.BtDel.Text = "删除";
this.BtDel.Click += new System.EventHandler(this.BtDel_Click);
//
// BtAdd
//
this.BtAdd.Location = new System.Drawing.Point(8, 24);
this.BtAdd.Name = "BtAdd";
this.BtAdd.TabIndex = 0;
this.BtAdd.Text = "添加";
this.BtAdd.Click += new System.EventHandler(this.BtAdd_Click);
//
// groupBox5
//
this.groupBox5.Controls.Add(this.toolBar1);
this.groupBox5.Location = new System.Drawing.Point(8, 8);
this.groupBox5.Name = "groupBox5";
this.groupBox5.Size = new System.Drawing.Size(224, 88);
this.groupBox5.TabIndex = 5;
this.groupBox5.TabStop = false;
this.groupBox5.Enter += new System.EventHandler(this.groupBox5_Enter);
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.Tbfirst,
this.Tbpre,
this.TBNext,
this.Tblast,
this.toolBarButton5,
this.TBExit});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(3, 17);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(218, 41);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// Tbfirst
//
this.Tbfirst.ImageIndex = 0;
this.Tbfirst.Text = "First";
this.Tbfirst.ToolTipText = "显示第一条";
//
// Tbpre
//
this.Tbpre.ImageIndex = 1;
this.Tbpre.Text = "Pre.";
this.Tbpre.ToolTipText = "显示上一条";
//
// TBNext
//
this.TBNext.ImageIndex = 2;
this.TBNext.Text = "Next";
this.TBNext.ToolTipText = "显示下一条";
//
// Tblast
//
this.Tblast.ImageIndex = 3;
this.Tblast.Text = "Last";
this.Tblast.ToolTipText = "显示最后一条";
//
// toolBarButton5
//
this.toolBarButton5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// TBExit
//
this.TBExit.ImageIndex = 4;
this.TBExit.Text = "Exit";
this.TBExit.ToolTipText = "退出";
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.ClientSize = new System.Drawing.Size(432, 654);
this.Controls.Add(this.groupBox5);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.GRDbooks);
this.Name = "Form1";
this.Text = "学生管理系统";
((System.ComponentModel.ISupportInitialize)(this.GRDbooks)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.groupBox4.ResumeLayout(false);
this.groupBox5.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void groupBox2_Enter(object sender, System.EventArgs e)
{
}
private void groupBox3_Enter(object sender, System.EventArgs e)
{
}
private void GRDbooks_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
}
private void txtSearch_TextChanged(object sender, System.EventArgs e)
{
}
private void groupBox5_Enter(object sender, System.EventArgs e)
{
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button==Tbfirst)
{
myrec.MoveFirst();
TBNext.Enabled=true;
}
else if(e.Button==Tbpre)
{
myrec.MovePrevious();
if(myrec.BOF)
{
Tbpre.Enabled=false;
return;
}
TBNext.Enabled=true;
}
else if(e.Button==TBNext)
{
myrec.MoveNext();
if(myrec.EOF)
{
TBNext.Enabled=false;
return;
}
Tbpre.Enabled=true;
}
else if(e.Button==Tblast)
{
myrec.MoveLast(0);
Tbpre.Enabled=true;
}
else if(e.Button==TBExit)
{
DialogResult myresult;
myresult=MessageBox.Show("是否确认退出?","信息框",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
if(myresult==DialogResult.OK)
{
Application.Exit();
}
else
{
return;
}
}
TxtName.Text=myrec.Fields[0].Value.ToString();
TxtNumber.Text=myrec.Fields[1].Value.ToString();
TxtPassword.Text=myrec.Fields[2].Value.ToString();
}
private void BtAdd_Click(object sender, System.EventArgs e)
{
String addSQLstr;
addSQLstr="select * from tblstudents where Number='" + TxtNumber.Text + "'";
myrec.Close();
myrec=Mydb.OpenRecordset(addSQLstr,DAO.RecordsetTypeEnum.dbOpenDynaset,DAO.RecordsetOptionEnum.dbDenyWrite,DAO.LockTypeEnum.dbOptimistic);
if(myrec.RecordCount>0)
{
MessageBox.Show("数据库中已经包含有该记录了,"+"如果要更改该记录请选择用编辑按钮","信息框",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
try
{
myrec.AddNew();
myrec.Fields[0].Value=TxtNumber.Text;
myrec.Fields[1].Value=TxtName.Text;
myrec.Fields[2].Value=TxtPassword.Text;
myrec.Update(1,false);
TxtNumber.Text="";
TxtName.Text="";
TxtPassword.Text="";
}
catch
{
MessageBox.Show("保存错误","错误");
}
myrec.Close();
OpenDb();
}
private void BtDel_Click(object sender, System.EventArgs e)
{
String DelSQLstr;
DelSQLstr="select * from tblstudents where Number='" + TxtNumber.Text +"'";
myrec.Close();
myrec=Mydb.OpenRecordset(DelSQLstr,DAO.RecordsetTypeEnum.dbOpenDynaset,DAO.RecordsetOptionEnum.dbDenyWrite,DAO.LockTypeEnum.dbOptimistic);
if(myrec.RecordCount==0)
{
MessageBox.Show("数据库中不存在此记录,无法完成删除操作。","信息框",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
myrec.Delete();
TxtNumber.Text="";
TxtName.Text="";
TxtPassword.Text="";
MessageBox.Show("删除成功!","恭喜你!");
myrec.Close();
OpenDb();
}
private void BtEdit_Click(object sender, System.EventArgs e)
{
String addSQLstr;
addSQLstr="select * from tblstudents where Number='" + TxtNumber.Text + "'";
myrec.Close();
myrec=Mydb.OpenRecordset(addSQLstr,DAO.RecordsetTypeEnum.dbOpenDynaset,DAO.RecordsetOptionEnum.dbDenyWrite,DAO.LockTypeEnum.dbOptimistic);
if(myrec.RecordCount==0)
{
MessageBox.Show("数据库中不包含有该记录,编辑操作失败。","信息框",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
myrec.Edit();
myrec.Fields[0].Value=TxtNumber.Text;
myrec.Fields[1].Value=TxtName.Text;
myrec.Fields[2].Value=TxtPassword.Text;
myrec.Update(1,false);
myrec.Close();
OpenDb();
}
private void BtSearch_Click(object sender, System.EventArgs e)
{
String SearchSQLStr="";
switch(Cmbtype.SelectedIndex)
{
case -1:
MessageBox.Show("请先选择查询类型。","查询操作失败",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
case 0:
if(txtSearch.Text.Length==0)
{
MessageBox.Show("请在文本框中输入查询的条件","操作失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
SearchSQLStr="select [Number],Name,[PassWord]" + "from(tblstudents)" + "where([Number]='" + txtSearch.Text + "')";
break;
case 1:
if(txtSearch.Text.Length==0)
{
MessageBox.Show("请在文本框中输入查询需要的条件","操作失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
SearchSQLStr="select [Number],Name,[PassWord]" + "from(tblstudents)" + "where(Name='" + txtSearch.Text + "')";
break;
case 2:
SearchSQLStr="select count(Name)" + "from(tblstudents)";
myrec=Mydb.OpenRecordset(SearchSQLStr,DAO.RecordsetTypeEnum.dbOpenDynaset,DAO.RecordsetOptionEnum.dbDenyWrite,DAO.LockTypeEnum.dbOptimistic);
MessageBox.Show("学生总人数为:" + myrec.Fields[0].Value.ToString(),"信息框",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
default:
break;
}
myrec.Close();
myrec=Mydb.OpenRecordset(SearchSQLStr,DAO.RecordsetTypeEnum.dbOpenDynaset,DAO.RecordsetOptionEnum.dbDenyWrite,DAO.LockTypeEnum.dbOptimistic);
if(myrec.RecordCount==0)
{
MessageBox.Show("没有查找到该学生的信息。","查询结果",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
TxtNumber.Text=myrec.Fields[0].Value.ToString();
TxtName.Text=myrec.Fields[1].Value.ToString();
TxtPassword.Text=myrec.Fields[2].Value.ToString();
}
private void BtSearchBooks_Click(object sender, System.EventArgs e)
{
string SearchSQLStr;
if (txtSearchNumber.Text.Length == 0)
{
MessageBox.Show("请在文本框中输入要查询的学号", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SearchSQLStr = "SELECT tblborrow.[Number]," + "tblborrow.BookID,tblborrow.BorrowData," + "tblbooks.bookName,tblbooks.State "
+ "FROM tblborrow INNER JOIN tblbooks ON " + "tblborrow.BookID = tblbooks.BookID " + " WHERE (tblborrow.[Number] = '" + txtSearchNumber.Text + "')";
System.Data.OleDb.OleDbDataAdapter adocmd;
adocmd =new OleDbDataAdapter(SearchSQLStr, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\lib.mdb");
DataSet ds = new DataSet();
adocmd.Fill(ds, "tblborrow");
GRDbooks.DataSource = ds.Tables[0].DefaultView;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -