📄 form1.cs
字号:
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(56, 23);
this.button8.TabIndex = 2;
this.button8.Text = "确定";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(352, 248);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(56, 23);
this.button9.TabIndex = 2;
this.button9.Text = "取消";
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(424, 248);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(56, 23);
this.button10.TabIndex = 2;
this.button10.Text = "返回";
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 310);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.label1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.label5);
this.Controls.Add(this.label6);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button4);
this.Controls.Add(this.button5);
this.Controls.Add(this.button6);
this.Controls.Add(this.button7);
this.Controls.Add(this.button8);
this.Controls.Add(this.button9);
this.Controls.Add(this.button10);
this.Name = "Form1";
this.Text = "软件0503班";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private BindingManagerBase Navigator;//用来对记录导航
private OleDbConnection MyCon;//连接对象
private OleDbCommand MyCom;//命令对象
private OleDbDataAdapter MyDataAdapter;//数据适配器对象
private DataSet MyDs;//数据集对象
private int CurRowNo;//当前行号,用来在修改操作后再次显示该行
private bool AppendRow;//表示是执行添加操作还是执行修改操作
private void TextReadOnly()//该方法使所有文本框不能修改
{
textBox1.ReadOnly =true;textBox2.ReadOnly =true;textBox3.ReadOnly =true;
textBox4.ReadOnly =true;textBox5.ReadOnly =true;textBox6.ReadOnly =true;
}
private void TextReadWrite()//该方法使所有文本框可以修改
{
textBox1.ReadOnly =false;textBox2.ReadOnly =false;textBox3.ReadOnly =false;
textBox4.ReadOnly =false;textBox5.ReadOnly =false;textBox6.ReadOnly =false ;
}
private void ClearText()//清除所有文本框的内容
{
textBox1.Text ="";textBox2.Text ="";textBox3.Text ="";
textBox4.Text ="";textBox5.Text ="";textBox6.Text ="";
}
private void BtnAppOrModi()//单击“添加”或“删除”按钮后各按钮的状态
{
button1.Enabled =false;button2.Enabled =false;button3.Enabled =false;
button4.Enabled =false;button5.Enabled =false;button6.Enabled =false;
button7.Enabled =false;button8.Enabled =true;
button9.Enabled =true;button10.Enabled =false;
}
private void BtnOkOrCancel()//单击确定与取消按钮后各按钮的状态
{
button1.Enabled =true;button2.Enabled =true;button3.Enabled =true;
button4.Enabled =true;button5.Enabled =true;button6.Enabled =true;
button7.Enabled =true;button8.Enabled =false;button9.Enabled =false;
button10.Enabled =true;
}
private void DispValue()//把当前记录的值显示在文本框中
{
textBox1.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["studentid"].ToString ();
textBox2.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["name"].ToString ();
textBox3.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["sex"].ToString ();
textBox4.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["classid"].ToString ();
textBox5.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["birthday"].ToString ();
textBox6.Text =MyDs.Tables ["Student"].Rows [Navigator.Position ]["native"].ToString ();
}
private void Form1_Load(object sender, System.EventArgs e)
{
string cstring ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=student.mdb";
string sqlstr="select * from student";
MyCon=new OleDbConnection (cstring);
MyCon.Open ();
MyCom=new OleDbCommand ();
MyCom.Connection =MyCon;
MyCom.CommandType=CommandType.Text ;
MyCom.CommandText =sqlstr;
MyDataAdapter=new OleDbDataAdapter ();
MyDataAdapter.SelectCommand =MyCom;
MyDs=new DataSet();
MyDataAdapter.Fill (MyDs,"student");
Navigator=this.BindingContext [MyDs,"student"];
BtnOkOrCancel();
TextReadOnly();
DispValue();
}
private void button1_Click(object sender, System.EventArgs e)
{
Navigator.Position =0;
DispValue();
}
private void button2_Click(object sender, System.EventArgs e)
{
if(Navigator.Position ==0)
Navigator.Position =Navigator.Count -1;
else
Navigator.Position -=1;
DispValue();
}
private void button3_Click(object sender, System.EventArgs e)
{
if(Navigator.Position ==Navigator.Count -1)
Navigator.Position =0;
else
Navigator.Position +=1;
DispValue();
}
private void button4_Click(object sender, System.EventArgs e)
{
Navigator.Position =Navigator.Count -1;
DispValue();
}
private void button5_Click(object sender, System.EventArgs e)
{
CurRowNo=Navigator.Position ;
TextReadWrite();
BtnAppOrModi();
ClearText();
AppendRow=true;
}
private void button6_Click(object sender, System.EventArgs e)
{
TextReadWrite();
BtnAppOrModi();
AppendRow=false;
CurRowNo=Navigator.Position ;
}
private void button7_Click(object sender, System.EventArgs e)
{
string DelString;//存放要执行的SQL语句的字符串
OleDbCommand MyComC = new OleDbCommand();//建立执行SQL语句的Command对象
MyComC.Connection =MyCon;//设置命令对象的Connection属性
MyComC.CommandType =CommandType.Text ;//设置命令类型
DelString="Delete From Student Where StudentID='"+textBox1.Text +"'";
MyComC.CommandText =DelString;//把该字符串作为命令文本
if (MessageBox.Show("真的要删除吗?","删除确认",MessageBoxButtons.OKCancel )==DialogResult.OK )
{
MyComC.ExecuteNonQuery();//执行SQL-Delete命令
MyDataAdapter.Update(MyDs,"Student");//更新操作,把记录集中的内容写入到数据源中
MyDs.Clear();//清除记录集
MyDataAdapter.Fill(MyDs,"student");//从数据源中提取信息
DispValue();
}
}
private void button8_Click(object sender, System.EventArgs e)
{
string AppString,ModiString;
OleDbCommand Mycomb=new OleDbCommand ();
Mycomb.Connection =MyCon;
Mycomb.CommandType =CommandType.Text ;
if(AppendRow==true)
{
AppString="Insert into student values('"+textBox1.Text +"','"+textBox2.Text +"','"+textBox3.Text +"','"+textBox4.Text +
"','"+textBox5.Text +"','"+textBox6.Text +"')";
Mycomb.CommandText =AppString;
Mycomb.ExecuteNonQuery ();
}
else
{
ModiString="Update student Set studentid='"+textBox1.Text +"'";
ModiString=ModiString+",name='"+textBox2.Text +"'";
ModiString=ModiString+",sex='"+textBox3.Text +"'";
ModiString=ModiString+",classid='"+textBox4.Text +"'";
ModiString=ModiString+",birthday='"+textBox5.Text +"'";
ModiString=ModiString+",native='"+textBox6.Text +"'";
Mycomb.CommandText =ModiString+"where studentid='"+textBox1.Text +"'";
Mycomb.ExecuteNonQuery ();
}
MyDataAdapter.Update (MyDs,"student");
MyDs.Clear ();
MyDataAdapter.Fill (MyDs,"student");
TextReadOnly();
BtnOkOrCancel();
if(AppendRow==true)
Navigator.Position =Navigator.Count -1;
else
Navigator.Position =CurRowNo;
DispValue();
}
private void button9_Click(object sender, System.EventArgs e)
{
TextReadOnly();
BtnOkOrCancel();
DispValue();
}
private void button10_Click(object sender, System.EventArgs e)
{
MyCon.Close ();
this.Close ();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -