实验四 数据库综合应用

【实验目的】

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

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

【实验要求】

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

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

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

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

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

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

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

 

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

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

 

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

 

代码
private static SqlConnection conn = null ;
private static SqlCommand cmd = null ;
CurrencyManager mycu;
public static DataTable dt = null ;
SqlDataAdapter da
= null ;
SqlCommandBuilder cb
= null ;
DataSet ds
= null ;
public static string a = "" ;
public static string b = "" ;
public static string 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了  其实不用静态也行 试试罢了

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

 

 

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

代码
private void 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、下一条

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

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

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

7、删除行

代码
private void 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上改,这个有不同于网页 网页是不能这样的 窗体太强了

 

代码
private void 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、增加记录

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

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

 

代码
public static bool 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 as object ),
new SqlParameter( " @name " ,name as object ),
new SqlParameter( " @title " ,title as object )
};
cmd
= new SqlCommand(str,getconn());
cmd.Parameters.AddRange(param);
int res = cmd.ExecuteNonQuery();
if (res > 0 )
{
return true ;
}
else {
return false ;
}
}

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

 

代码
private void 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了 。。。。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/huaizuo/archive/2010/12/25/1916994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值