2023年6月21日发(作者:)
实验七 数据库操作技术实验步骤
1、 新建数据库StaffDataBase
2、 新建数据表 设置主键
3、新建职工信息操作窗口
(1) 添加bindingNavigator控件,添加三个toolStripButton,命名为“添加”、“修改”、“删除”
(2) 把DisplayStyle属性值改成ImageAndText
(3) 添加dataGridView
(4) 得到完整窗口
4.新建职工信息编辑窗口
5、1)定义一个数据库连接类
完成连接类,代码如下(导入包using ent);
):
using System;
using c;
using ;
using ;
using ent;
namespace WindowsFormsApplication2
{
class DataBaseLink
{
public string strConn = "Data Source=cc-PCsqlexpress;Initial
Catalog=StaffDataBase;Integrated Security=True"; /*链接字符串*/
public SqlConnection DataLink()
{
SqlConnection con = new SqlConnection(strConn); /*/实例化连接对象*/
return con; /*返回链接对象*/
}
}
}
6.新建接口InterfaceDataAccessing
完成接口类,代码为(导入包using );
):
using System;
using c;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
interface InterfaceDataAccessing
{
void Add(String[] st); /*添加数据到数据库,参数接受信息*/
void Updata(String[] st, string id); /*更新数据,参数1接收修改的信息,参数2修改条件*/
void Delete(string id); /* 删除数据,参数删除条件*/
DataSet GetData(string st);/*查询数据,参数为查询条件*/
}
}
7.添加职工类去继承接口:
对职工类去继承接口并进行操作方法的编写为:
using System;
using c;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
class Staff
{
DataBaseLink db = new DataBaseLink();/*实例化类的对象*/
public void Add(String[] st)
{
using (SqlConnection con = nk()) /* 返回连接*/
{
if ( == )/*若数据库状态为关则打开*/
{
();
}
try
{ string sql = "Insert into Staff(bh,xm,xb,byxy,xl,xw,gzbm,lxdz,yxdz,zz)
Values(@bh,@xm,@xb,@byxy,@xl,@xw,@gzbm,@lxdz,@yxdz,@zz)";//查询语句同学也可以参考教材上的例子编写查询语句,所以字段,所有的值
SqlCommand cmd = new SqlCommand(sql, con);/*创建查询命令对象,参数1查询语句,参数2连接对象*/
SqlParameter p1 = new SqlParameter("@bh", st[0]);/*结构化查询参数,参数1值变量,参数2,值的具体内容*/
SqlParameter p2 = new SqlParameter("@xm", st[1]);
SqlParameter p3 = new SqlParameter("@xb", st[2]);
SqlParameter p4 = new SqlParameter("@byxy", st[3]);
SqlParameter p5 = new SqlParameter("@xl", st[4]);
SqlParameter p6 = new SqlParameter("@xw", st[5]);
SqlParameter p7 = new SqlParameter("@gzbm", st[6]);
SqlParameter p8 = new SqlParameter("@lxdz", st[7]);
SqlParameter p9 = new SqlParameter("@yxdz", st[8]);
SqlParameter p10 = new SqlParameter("@zz", st[9]);
(p1);/*把新创建的对象添加到*/
(p2);
(p3);
(p4);
(p5);
(p6);
(p7);
(p8);
(p9);
(p10);
eNonQuery();//执行查询命令
("信息添加成功!");
}
catch (Exception ex)//异常放到ex中
{
("错误:" + e, "错误提示",
el, );
}
finally /*不管连接对象状态是否关闭,都执行关闭连接*/
{
if ( == )
{
();
e();/*释放连接对象*/
}
}
} }
public DataSet GetData(string st) /*定义方法,返回类型数据集,参数为查询条件*/
{
DataSet ds = new DataSet("Staff");
if(st==null)/*判断查询条件是否为空,全查询*/
{
using (SqlConnection con = nk())/*返回连接对象*/
{
if ( == )
{
();
}
try
{
string sql = "Select * From Staff";/*查询语句*/
SqlDataAdapter sqlda = new SqlDataAdapter(sql,con);/*新建查询适配器,参数1查询语句,参数2连接对象*/
(ds);/*执行查询并填充*/
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();
}
}
}
}
return ds;
}
public void Delete(string id) /*定义删除方法,返回值类型为空,参数为删除条件*/
{
using (SqlConnection con = nk())/*使用连接对象,定义连接变量,返回连接对象*/
{ if ( == )
{
();
}
try
{
string sql = "Delete From Staff Where bh=" + "'"+id+"'";/*定义查询语句*/
SqlCommand comd = new SqlCommand(sql,con);/*创建新的命令对象,参数1删除命令,参数2为连接对象*/
eNonQuery();//执行指令
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();
}
}
}
}
public void Updata(String[] st,string id) /*定义更新数据方法,参数1修改内容,参数2修改条件*/
{
using (SqlConnection con = nk())/*使用连接对象,定义连接变量,变量接受返回的连接对象*/
{
if ( == )
{
();
}
try
{
string sql = "Update Staff set bh=@bh,xm=@xm,xb=@xb,byxy=@byxy,xl=@xl,xw=@xw,gzbm=@gzbm,lxdz=@lxdz,yxdz=@yxdz,zz=@zz where
bh='"+id+"'";
SqlCommand cmd = new SqlCommand(sql, con);/* 创建查询命令对象,参数1查询语句,参数2连接对象*/
SqlParameter p1 = new SqlParameter("@bh", st[0]); ;
SqlParameter p2 = new SqlParameter("@xm", st[1]); /*结构化查询参数,参数1值变量,参数2值的具体内容*/
SqlParameter p3 = new SqlParameter("@xb", st[2]);
SqlParameter p4 = new SqlParameter("@byxx", st[3]);
SqlParameter p5 = new SqlParameter("@xl", st[4]);
SqlParameter p6 = new SqlParameter("@xw", st[5]);
SqlParameter p3 = new SqlParameter("@gzbm", st[6]);
SqlParameter p4 = new SqlParameter("@lxdz", st[7]);
SqlParameter p5 = new SqlParameter("@yxdz", st[8]);
SqlParameter p6 = new SqlParameter("@zz", st[9]);
(p1); /*把新创建的对象添加到数据集和中*/
(p2);
(p3);
(p4);
(p5);
(p6);
(p7);
(p8);
(p9);
(p10);
eNonQuery();/*执行指令*/
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();/*释放连接对象*/
}
} }
}
}
}
8.编写StaffFrom窗口的相关程序代码如下:
using System;
using c;
using entModel;
using ;
using g;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
public partial class StaffFrom : Form
{
private BindingSource bindingSource = new BindingSource();/*Bindingsource是显示控件与数据集中的数据表的中间层*/
Staff std = new Staff();/*创建学生对象*/
public StaffFrom()
{
InitializeComponent();
DataBinding();/*方法,实现数据绑定*/
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1("xz", null);
alog();
DataBinding();
}
public void DataBinding() /*定义方法*/
{
DataSet ds = a(null);
urce = [0];
gSource = bindingSource; urce = bindingSource;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
DataGridViewRow dataRow = edRows[0];
if (dataRow ==null )
{
("请选中要修改的数据!", "提示");
}
else
{
Form1 form = new Form1("xg", dataRow);
alog();
DataBinding();
}
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
DialogResult dia = ("你确实要删除此记录吗", "确认删除",
el, g);
if (dia == )
{
DataGridViewRow dataRow = edRows[0];
([0].ng());
DataBinding();
}
}
}
}
完整窗口为:
9.编写From1窗口的相关程序代码如下:
using System;
using c;
using entModel;
using ;
using g;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string str;
string id;
public Form1(string str,DataGridViewRow dgvr)
{
InitializeComponent();
= str;
if(str == "xg")
{
= "修改";
= [0].ng();
= [1].ng();
= [2].ng();
= [3].ng();
= [4].ng();
= [5].ng();
= [6].ng();
= [7].ng();
= [8].ng();
= [9].ng();
id = [0].ng();
d = false;
}
if(str == "xz")
{
= "新增";
}
}
private void button1_Click(object sender, EventArgs e) {
string[] st = new string[10];
if(str == "xz")
{
st[0] = ();
st[1] = ();
st[2] = ();
st[3] = ();
st[4] = ();
st[5] = ();
st[6] = ();
st[7] = ();
st[8] = ();
st[9] = ();
Staff idaxz = new Staff();
(st);
();
}
if(str == "xg")
{
st[0] = ();
st[1] = ();
st[2] = ();
st[3] = ();
st[4] = ();
st[5] = ();
st[6] = ();
st[7] = ();
st[8] = ();
st[9] = ();
Staff idaxg = new Staff();
(st,id);
();
}
}
private void button2_Click(object sender, EventArgs e)
{
();
}
}
} 完整后的窗口为:
10.运行程序:
1.单击添加:
(1).输入数据
(2).点击“新增”后:
2.单击修改: (1).输入修改数据:
(2).单击“修改”后
3.单击删除:
(1).删除数据:
(2).点击取消:
(3).点击确定:
数据库的分离:
2023年6月21日发(作者:)
实验七 数据库操作技术实验步骤
1、 新建数据库StaffDataBase
2、 新建数据表 设置主键
3、新建职工信息操作窗口
(1) 添加bindingNavigator控件,添加三个toolStripButton,命名为“添加”、“修改”、“删除”
(2) 把DisplayStyle属性值改成ImageAndText
(3) 添加dataGridView
(4) 得到完整窗口
4.新建职工信息编辑窗口
5、1)定义一个数据库连接类
完成连接类,代码如下(导入包using ent);
):
using System;
using c;
using ;
using ;
using ent;
namespace WindowsFormsApplication2
{
class DataBaseLink
{
public string strConn = "Data Source=cc-PCsqlexpress;Initial
Catalog=StaffDataBase;Integrated Security=True"; /*链接字符串*/
public SqlConnection DataLink()
{
SqlConnection con = new SqlConnection(strConn); /*/实例化连接对象*/
return con; /*返回链接对象*/
}
}
}
6.新建接口InterfaceDataAccessing
完成接口类,代码为(导入包using );
):
using System;
using c;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
interface InterfaceDataAccessing
{
void Add(String[] st); /*添加数据到数据库,参数接受信息*/
void Updata(String[] st, string id); /*更新数据,参数1接收修改的信息,参数2修改条件*/
void Delete(string id); /* 删除数据,参数删除条件*/
DataSet GetData(string st);/*查询数据,参数为查询条件*/
}
}
7.添加职工类去继承接口:
对职工类去继承接口并进行操作方法的编写为:
using System;
using c;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
class Staff
{
DataBaseLink db = new DataBaseLink();/*实例化类的对象*/
public void Add(String[] st)
{
using (SqlConnection con = nk()) /* 返回连接*/
{
if ( == )/*若数据库状态为关则打开*/
{
();
}
try
{ string sql = "Insert into Staff(bh,xm,xb,byxy,xl,xw,gzbm,lxdz,yxdz,zz)
Values(@bh,@xm,@xb,@byxy,@xl,@xw,@gzbm,@lxdz,@yxdz,@zz)";//查询语句同学也可以参考教材上的例子编写查询语句,所以字段,所有的值
SqlCommand cmd = new SqlCommand(sql, con);/*创建查询命令对象,参数1查询语句,参数2连接对象*/
SqlParameter p1 = new SqlParameter("@bh", st[0]);/*结构化查询参数,参数1值变量,参数2,值的具体内容*/
SqlParameter p2 = new SqlParameter("@xm", st[1]);
SqlParameter p3 = new SqlParameter("@xb", st[2]);
SqlParameter p4 = new SqlParameter("@byxy", st[3]);
SqlParameter p5 = new SqlParameter("@xl", st[4]);
SqlParameter p6 = new SqlParameter("@xw", st[5]);
SqlParameter p7 = new SqlParameter("@gzbm", st[6]);
SqlParameter p8 = new SqlParameter("@lxdz", st[7]);
SqlParameter p9 = new SqlParameter("@yxdz", st[8]);
SqlParameter p10 = new SqlParameter("@zz", st[9]);
(p1);/*把新创建的对象添加到*/
(p2);
(p3);
(p4);
(p5);
(p6);
(p7);
(p8);
(p9);
(p10);
eNonQuery();//执行查询命令
("信息添加成功!");
}
catch (Exception ex)//异常放到ex中
{
("错误:" + e, "错误提示",
el, );
}
finally /*不管连接对象状态是否关闭,都执行关闭连接*/
{
if ( == )
{
();
e();/*释放连接对象*/
}
}
} }
public DataSet GetData(string st) /*定义方法,返回类型数据集,参数为查询条件*/
{
DataSet ds = new DataSet("Staff");
if(st==null)/*判断查询条件是否为空,全查询*/
{
using (SqlConnection con = nk())/*返回连接对象*/
{
if ( == )
{
();
}
try
{
string sql = "Select * From Staff";/*查询语句*/
SqlDataAdapter sqlda = new SqlDataAdapter(sql,con);/*新建查询适配器,参数1查询语句,参数2连接对象*/
(ds);/*执行查询并填充*/
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();
}
}
}
}
return ds;
}
public void Delete(string id) /*定义删除方法,返回值类型为空,参数为删除条件*/
{
using (SqlConnection con = nk())/*使用连接对象,定义连接变量,返回连接对象*/
{ if ( == )
{
();
}
try
{
string sql = "Delete From Staff Where bh=" + "'"+id+"'";/*定义查询语句*/
SqlCommand comd = new SqlCommand(sql,con);/*创建新的命令对象,参数1删除命令,参数2为连接对象*/
eNonQuery();//执行指令
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();
}
}
}
}
public void Updata(String[] st,string id) /*定义更新数据方法,参数1修改内容,参数2修改条件*/
{
using (SqlConnection con = nk())/*使用连接对象,定义连接变量,变量接受返回的连接对象*/
{
if ( == )
{
();
}
try
{
string sql = "Update Staff set bh=@bh,xm=@xm,xb=@xb,byxy=@byxy,xl=@xl,xw=@xw,gzbm=@gzbm,lxdz=@lxdz,yxdz=@yxdz,zz=@zz where
bh='"+id+"'";
SqlCommand cmd = new SqlCommand(sql, con);/* 创建查询命令对象,参数1查询语句,参数2连接对象*/
SqlParameter p1 = new SqlParameter("@bh", st[0]); ;
SqlParameter p2 = new SqlParameter("@xm", st[1]); /*结构化查询参数,参数1值变量,参数2值的具体内容*/
SqlParameter p3 = new SqlParameter("@xb", st[2]);
SqlParameter p4 = new SqlParameter("@byxx", st[3]);
SqlParameter p5 = new SqlParameter("@xl", st[4]);
SqlParameter p6 = new SqlParameter("@xw", st[5]);
SqlParameter p3 = new SqlParameter("@gzbm", st[6]);
SqlParameter p4 = new SqlParameter("@lxdz", st[7]);
SqlParameter p5 = new SqlParameter("@yxdz", st[8]);
SqlParameter p6 = new SqlParameter("@zz", st[9]);
(p1); /*把新创建的对象添加到数据集和中*/
(p2);
(p3);
(p4);
(p5);
(p6);
(p7);
(p8);
(p9);
(p10);
eNonQuery();/*执行指令*/
}
catch (Exception ex)
{
("错误:" + e, "错误提示",
el, );
}
finally
{
if ( == )
{
();
e();/*释放连接对象*/
}
} }
}
}
}
8.编写StaffFrom窗口的相关程序代码如下:
using System;
using c;
using entModel;
using ;
using g;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
public partial class StaffFrom : Form
{
private BindingSource bindingSource = new BindingSource();/*Bindingsource是显示控件与数据集中的数据表的中间层*/
Staff std = new Staff();/*创建学生对象*/
public StaffFrom()
{
InitializeComponent();
DataBinding();/*方法,实现数据绑定*/
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1("xz", null);
alog();
DataBinding();
}
public void DataBinding() /*定义方法*/
{
DataSet ds = a(null);
urce = [0];
gSource = bindingSource; urce = bindingSource;
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
DataGridViewRow dataRow = edRows[0];
if (dataRow ==null )
{
("请选中要修改的数据!", "提示");
}
else
{
Form1 form = new Form1("xg", dataRow);
alog();
DataBinding();
}
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
DialogResult dia = ("你确实要删除此记录吗", "确认删除",
el, g);
if (dia == )
{
DataGridViewRow dataRow = edRows[0];
([0].ng());
DataBinding();
}
}
}
}
完整窗口为:
9.编写From1窗口的相关程序代码如下:
using System;
using c;
using entModel;
using ;
using g;
using ;
using ;
using ;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string str;
string id;
public Form1(string str,DataGridViewRow dgvr)
{
InitializeComponent();
= str;
if(str == "xg")
{
= "修改";
= [0].ng();
= [1].ng();
= [2].ng();
= [3].ng();
= [4].ng();
= [5].ng();
= [6].ng();
= [7].ng();
= [8].ng();
= [9].ng();
id = [0].ng();
d = false;
}
if(str == "xz")
{
= "新增";
}
}
private void button1_Click(object sender, EventArgs e) {
string[] st = new string[10];
if(str == "xz")
{
st[0] = ();
st[1] = ();
st[2] = ();
st[3] = ();
st[4] = ();
st[5] = ();
st[6] = ();
st[7] = ();
st[8] = ();
st[9] = ();
Staff idaxz = new Staff();
(st);
();
}
if(str == "xg")
{
st[0] = ();
st[1] = ();
st[2] = ();
st[3] = ();
st[4] = ();
st[5] = ();
st[6] = ();
st[7] = ();
st[8] = ();
st[9] = ();
Staff idaxg = new Staff();
(st,id);
();
}
}
private void button2_Click(object sender, EventArgs e)
{
();
}
}
} 完整后的窗口为:
10.运行程序:
1.单击添加:
(1).输入数据
(2).点击“新增”后:
2.单击修改: (1).输入修改数据:
(2).单击“修改”后
3.单击删除:
(1).删除数据:
(2).点击取消:
(3).点击确定:
数据库的分离:
发布评论