实验七数据库操作技术.docx
《实验七数据库操作技术.docx》由会员分享,可在线阅读,更多相关《实验七数据库操作技术.docx(14页珍藏版)》请在冰点文库上搜索。
实验七数据库操作技术
大理学院课程教案
(实验教学)
课程名称:
C#面向对象程序设计
课程类型:
(1)1、必修;2、选修;3、其它
授课对象:
计算机科学与技术专业(本、专科)2010级1班
授课时间:
2011至2012学年第3学期
计划学时:
36学时(其中:
理论36,实验:
36)
任课教师:
杜英国
所属学院:
数学与计算机学院
课程管理部门(教研室):
软件教研室
大理学院教务处制
课程名称:
C#面向对象程序设计
教材:
C#程序设计实用教程黄兴荣李昌领李继良编著清华大学出版社
授课人1:
杜英国专业技术职务:
讲师
学历:
研究生学位:
硕士
授课人2:
专业技术职务:
学历:
学位:
实验题目:
C#集成开发环境控制台输入输出程序设计计划学时:
3
实验类型:
(4)1、演示性2、验证性3、综合性4、设计性
每组实验的学生人数:
1人
教学目的和要求:
掌握SqlConnection类、SqlCommand命令、DataTable类、DataSet类、DataReader类、DataGridView控件、BindingNavigator控件、BindingSource类的使用。
实验方法(包括实验中需要注意的问题等):
1.在VS下,新建一个窗体界面应用程序,完成学生基本信息的添加,删除,修改功能。
2.定义一个数据库连接类。
3.定义一个接口来描述数据库操作的常用方法。
4.定义一个Student类完成对学生基本信息的操作,该类继承接口InterfaceDataAccessing。
5.搭建一个studentForm窗体来显示学生基本信息。
实验重点(主要解决的问题和达到的目的):
1.学会编ADO.NET操作代码。
2.理解数据库应用程序编程相关的类和控件的使用。
实验难点(预计实验过程中会遇到的问题和解决方案):
1.编写ADO.NET操作代码。
教学方法(实验前的教学和实验过程中的指导方法):
实验前教师先讲解完数据库编程相关内容,学生认真复习数据库编程相关内容;在实验过程中结合实验环境教师可先提示性讲解实验内容,再由学生自己完成实验。
如果实验完成情况较差,教师统一辅导。
实验仪器和材料:
计算机,WindowsXP,VS2008
实验报告要求和思考题:
提交实验报告。
参考资料:
《C#程序设计项目教程—实验指导与课程设计》黄兴荣编著清华大学出版社
实验七数据库操作技术
一、实验内容与步骤(要求交实验报告的实验项目详细步骤由学生填写)
实验内容:
1.按要求调试下列程序:
功能:
完成学生基本信息的添加,删除,修改功能。
1)定义一个数据库连接类
classDataBaseLink
{
publicstringstrConn="DataSource=localhost;InitialCatalog=School;IntegratedSecurity=true";
publicSqlConnectionDataLink()
{
SqlConnectioncon=newSqlConnection(strConn);
returncon;
}
}
2)定义一个接口来描述数据库操作的常用方法
publicinterfaceInterfaceDataAccessing
{
voidAdd(String[]st);
voidUpdata(String[]st,stringid);
voidDelete(stringid);
DataSetGetData(stringst);
}
3)定义一个Student类完成对学生基本信息的操作,该类继承接口InterfaceDataAccessing
publicclassStudent:
InterfaceDataAccessing
{
DataBaseLinkdb=newDataBaseLink();
publicvoidAdd(String[]st)
{
using(SqlConnectioncon=db.DataLink())
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
try
{
stringsql="Insertintostudent(xh,xm,xb,jg,xy,bj)Values(@xh,@xm,@xb,@jg,@xy,@bj)";//查询语句同学也可以参考教材上的例子编写
SqlCommandcmd=newSqlCommand(sql,con);
SqlParameterp1=newSqlParameter("@xh",st[0]);
SqlParameterp2=newSqlParameter("@xm",st[1]);
SqlParameterp3=newSqlParameter("@xb",st[2]);
SqlParameterp4=newSqlParameter("@jg",st[3]);
SqlParameterp5=newSqlParameter("@xy",st[4]);
SqlParameterp6=newSqlParameter("@bj",st[5]);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
cmd.Parameters.Add(p5);
cmd.Parameters.Add(p6);
cmd.ExecuteNonQuery();
MessageBox.Show("信息添加成功!
");
}
catch(Exceptionex)
{
MessageBox.Show("错误:
"+ex.Message,"错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
finally
{
if(con.State==ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
}
publicDataSetGetData(stringst)
{
DataSetds=newDataSet("student");
if(st==null)
{
using(SqlConnectioncon=db.DataLink())
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
try
{
stringsql="Select*FromStudent";
SqlDataAdaptersqlda=newSqlDataAdapter(sql,con);
sqlda.Fill(ds);
}
catch(Exceptionex)
{
MessageBox.Show("错误:
"+ex.Message,"错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
finally
{
if(con.State==ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
}
returnds;
}
publicvoidDelete(stringid)
{
using(SqlConnectioncon=db.DataLink())
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
try
{
stringsql="DeleteFromStudentWherexh="+"'"+id+"'";
SqlCommandcomd=newSqlCommand(sql,con);
comd.ExecuteNonQuery();
}
catch(Exceptionex)
{
MessageBox.Show("错误:
"+ex.Message,"错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
finally
{
if(con.State==ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
}
publicvoidUpdata(String[]st,stringid)
{
using(SqlConnectioncon=db.DataLink())
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
try
{
stringsql="Updatestudentsetxh=@xh,xm=@xm,xb=@xb,jg=@jg,xy=@xy,bj=@bjwherexh="+"'"+id+"'";
SqlCommandcmd=newSqlCommand(sql,con);
SqlParameterp1=newSqlParameter("@xh",st[0]);
SqlParameterp2=newSqlParameter("@xm",st[1]);
SqlParameterp3=newSqlParameter("@xb",st[2]);
SqlParameterp4=newSqlParameter("@jg",st[3]);
SqlParameterp5=newSqlParameter("@xy",st[4]);
SqlParameterp6=newSqlParameter("@bj",st[5]);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
cmd.Parameters.Add(p5);
cmd.Parameters.Add(p6);
cmd.ExecuteNonQuery();
}
catch(Exceptionex)
{
MessageBox.Show("错误:
"+ex.Message,"错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
finally
{
if(con.State==ConnectionState.Open)
{
con.Close();
con.Dispose();
}
}
}
}
}
4)搭建一个studentForm窗体来显示学生基本信息
publicpartialclassstudentForm:
Form
{
privateBindingSourcebindingSource=newBindingSource();
Studentstd=newStudent();
publicstudentForm()
{
InitializeComponent();
DataBinding();
}
privatevoidtoolStripButton1_Click(objectsender,EventArgse)
{
Form1form1=newForm1("xz",null);
form1.ShowDialog();
DataBinding();
}
publicvoidDataBinding()
{
DataSetds=std.GetData(null);
bindingSource.DataSource=ds.Tables[0];
bindingNavigator1.BindingSource=bindingSource;
dataGridView1.DataSource=bindingSource;
}
privatevoidtoolStripButton2_Click(objectsender,EventArgse)
{
DialogResultdia=MessageBox.Show("你确实要删除此记录吗","确认删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
if(dia==DialogResult.OK)
{
DataGridViewRowdataRow=dataGridView1.SelectedRows[0];
std.Delete(dataRow.Cells[0].Value.ToString());
DataBinding();
}
}
privatevoidtoolStripButton3_Click(objectsender,EventArgse)
{
DataGridViewRowdataRow=dataGridView1.SelectedRows[0];
Form1form=newForm1("xg",dataRow);
form.ShowDialog();
DataBinding();
}
}
5)构造如图所示界面来编辑学生基本信息
publicpartialclassForm1:
Form
{
stringstr;
stringid;
publicForm1(stringstr,DataGridViewRowdgvr)
{
InitializeComponent();
this.str=str;
if(str=="xg")
{
button1.Text="修改";
textBox1.Text=dgvr.Cells[0].Value.ToString();
textBox2.Text=dgvr.Cells[1].Value.ToString();
textBox3.Text=dgvr.Cells[2].Value.ToString();
comboBox1.Text=dgvr.Cells[3].Value.ToString();
comboBox2.Text=dgvr.Cells[4].Value.ToString();
comboBox3.Text=dgvr.Cells[5].Value.ToString();
id=dgvr.Cells[0].Value.ToString();
textBox1.Enabled=false;
}
if(str=="xz")
{
button1.Text="新增";
}
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
string[]st=newstring[6];
if(str=="xz")
{
st[0]=textBox1.Text.Trim();
st[1]=textBox2.Text.Trim();
st[2]=textBox3.Text.Trim();
st[3]=comboBox1.Text.Trim();
st[4]=comboBox2.Text.Trim();
st[5]=comboBox3.Text.Trim();
InterfaceDataAccessingidaxz=newStudent();
idaxz.Add(st);
this.Close();
}
if(str=="xg")
{
st[0]=textBox1.Text.Trim();
st[1]=textBox2.Text.Trim();
st[2]=textBox3.Text.Trim();
st[3]=comboBox1.Text.Trim();
st[4]=comboBox2.Text.Trim();
st[5]=comboBox3.Text.Trim();
InterfaceDataAccessingidaxg=newStudent();
idaxg.Updata(st,id);
this.Close();
}
}
}
2.参考上述程序,编写一个完成职工信息操作的数据库应用程序。
职工信息存储在StaffDataBase的Staff数据表中;职工信息包括:
编号,姓名,性别,毕业学校,学历,学位,工作部门,联系电话,邮箱地址,住址等。
实现职工信息的存储、查询、删除,修改操作。