实验四 数据库综合应用

【实验目的】

1.了解ADO.NET数据库访问机制;

2.熟练掌握利用数据适配器、数据集、命令和数据阅读器对数据库进行查询、删除、更新和添加操作;熟练掌握数据绑定

【实验要求】

1.是一个windows的数据库综合应用,功能自定义。

2.利用数据库连接、数据集和数据适配器对数据库进行四种操作。

3.利用数据库连接、命令和数据阅读器器对数据库进行四种操作。

4.体验两种不同数据库访问方法的优劣及其使用场合。

5.要有数据绑定和数据导航功能。

【实验步骤】(要求自己填写详细的实验步骤,设计思路和关键代码)

【实验体会及存在问题】(要求自己填写,感想、设计时碰到的问题,包括设计思想、调试等)

 

 实验步骤:先看看运行结果吧 下面这张图是主界面

当点击增加记录的时候 会跳到子界面 Form2  这里我用到了全局变量来实现两个窗体之间的传参:

 

1、定义Form1的字段和全局变量

 

代码
privatestatic SqlConnection conn =null;
privatestatic SqlCommand cmd =null;
CurrencyManager mycu;
publicstatic DataTable dt =null;
SqlDataAdapter da
=null;
SqlCommandBuilder cb
=null;
DataSet ds
=null;
publicstaticstring a ="";
publicstaticstring b ="";
publicstaticstring c ="";

2、定义Form1的构造函数

代码
public Form1()
{
string strconn =" Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;pwd=123456";
conn
=new SqlConnection(strconn);
InitializeComponent();
}

3、打开数据库的链接  这里我是静态的 这样在Form2就不用实例化Form1了  其实不用静态也行 试试罢了

代码
publicstatic SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}

 

 

 4、当加载Form1的时候开始绑定datagridview、textbox和combobox控件

代码
privatevoid Form1_Load(object sender, EventArgs e)
{
string str ="select * from Customers";
dt
=new DataTable();
da
=new SqlDataAdapter(str, getconn());
da.Fill(dt);
dataGridView1.DataSource
= dt;
////////////////////////////////////////////////////////////
mycu = (CurrencyManager)BindingContext[dt];
/////////////////////////
comboBox1.DataSource = dt;
comboBox1.DisplayMember
="City";

textBox1.DataBindings.Add(
"text", dt, "CompanyName");

}

5、下一条

代码
privatevoid button1_Click(object sender, EventArgs e)
{
mycu.Position
+=1;
if (mycu.Position==dt.Rows.Count-1)
{
MessageBox.Show(
"已经是最后一条了");
mycu.Position
=0;
}
}

6、删除行的函数 我这里是自己写sql删除语句的 后来我知道不写也行 可以用适配器的delete()

代码
privatebool del_Rows(string id) {
string str ="delete from Customers where CustomerID='"+ id +"'";
cmd
=new SqlCommand(str,getconn());
int res = cmd.ExecuteNonQuery();
if (res >0)
{
returntrue;
}
else {
returnfalse;
}
}

7、删除行

代码
privatevoid btn_del_Click(object sender, EventArgs e)
{

bool b = del_Rows(dataGridView1.CurrentRow.Cells["CustomerID"].Value.ToString());
if (b)
{
MessageBox.Show(
"success");
dataGridView1.DataSource
= dt;
}
else {
MessageBox.Show(
"error");
}
}

8、下面是实现datagridview的修改 这个修改是直接在datagridview上改,这个有不同于网页 网页是不能这样的 窗体太强了

 

代码
privatevoid button2_Click(object sender, EventArgs e)
{
BindingContext[dt].EndCurrentEdit();
cb
=new SqlCommandBuilder(da);
da.UpdateCommand
= cb.GetUpdateCommand();
if (da.Update(dt)>0) {
MessageBox.Show(
"修改成功!");
}
}

9、增加记录

privatevoid button3_Click(object sender, EventArgs e)
{
Form2 f2
=new Form2(this);
f2.Owner
=this;
f2.Show();
}

10、添加函数 当Form2窗体点击确定的时候就调用这个函数 实现真正的添加

 

代码
publicstaticbool insert(string id, string name, string title)
{
string str ="insert into Customers(customerid,companyname,contacttitle) values(@id,@name,@title)";
SqlParameter[] param
=new SqlParameter[] {
new SqlParameter("@id",id asobject),
new SqlParameter("@name",name asobject),
new SqlParameter("@title",title asobject)
};
cmd
=new SqlCommand(str,getconn());
cmd.Parameters.AddRange(param);
int res = cmd.ExecuteNonQuery();
if (res >0)
{
returntrue;
}
else {
returnfalse;
}
}

11、这边来介绍下Form2窗体把  也就是实现增加记录的窗体

 

代码
privatevoid button1_Click(object sender, EventArgs e)
{
//Form1 f1 = this.Owner as Form1;
//MessageBox.Show(f1.comboBox1.Text);
Form1.a += textBox1.Text;
Form1.b
+= textBox2.Text;
Form1.c
+= textBox3.Text;
bool b=Form1.insert(Form1.a,Form1.b,Form1.c);
if (b)
{
MessageBox.Show(
"添加成功");
string str ="select * from Customers";
DataTable dt
=new DataTable();
SqlDataAdapter da
=new SqlDataAdapter(str, Form1.getconn());
da.Fill(dt);
new Form1().dataGridView1.DataSource = dt;
}
else {
MessageBox.Show(
"添加失败");
}
}

这样 实验四就算ok了 。。。。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2010-12-25 23:14  小霖2012  阅读(615)  评论(0编辑  收藏  举报