实验七 ADO.NET数据库访问技术(二)
一. 目的和要求
掌握DataReader, DataSet, DataTable, DataAdapter以及DataView对象. 掌握使用存储过程的方法.
二.实验课时
2课时。
三.实验内容
1. 编写程序,创建一个使用DataGrid控件和DataSet显示位于”pubs”数据库中的”jobs”表中的所有记录的Web应用程序。
2. 编写程序,创建一个使用SqlAdapter对象对pubs数据库中的”jobs”表进行增加、删除和修改记录的Web应用程序。
3. 编写程序,创建一个DataView对象,在’pubs’数据库中的“employee“表查找job_id>10并按fname降序输出相应的记录。
4. 编写程序,在pubs数据库中创建一存储过程,用来显示employee表中所有记录.然后在ADO.NET中执行该存储过程. (存储过程如下:)
1. DataAdapter对象的作用是什么?
SqlDataAdapter是 DataSet和 SQL Server之间的桥接器,用于检索和保存数据。
2. DataReader的特点是什么?
DataReader从数据源中一条一条的读取数据,整个过程都要与数据库保持连接。
一. 目的和要求
掌握DataReader, DataSet, DataTable, DataAdapter以及DataView对象. 掌握使用存储过程的方法.
二.实验课时
2课时。
三.实验内容
1. 编写程序,创建一个使用DataGrid控件和DataSet显示位于”pubs”数据库中的”jobs”表中的所有记录的Web应用程序。
2. 编写程序,创建一个使用SqlAdapter对象对pubs数据库中的”jobs”表进行增加、删除和修改记录的Web应用程序。
3. 编写程序,创建一个DataView对象,在’pubs’数据库中的“employee“表查找job_id>10并按fname降序输出相应的记录。
4. 编写程序,在pubs数据库中创建一存储过程,用来显示employee表中所有记录.然后在ADO.NET中执行该存储过程. (存储过程如下:)
create procedure aaa as select * from employee
代码:
//本代码按照实验要求写的,不过有些地方为了减少几十行的代码用了十分猥琐的方法。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace testSqlDataAdapter
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
string command = "select * from spt_values";
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"Server=.;Database=jobs;Integrated Security=SSPI;");
string command = "select * from jobs";
SqlDataAdapter adp = new SqlDataAdapter(command, con);
con.Open();
DataSet ds = new DataSet();
adp.Fill(ds, "jobs");
GridView1.DataSource = ds.Tables["jobs"];
GridView1.DataBind();
con.Close();
}
/// <summary>
/// 插入数据
/// </summary>
void insert()
{
SqlDataAdapter adp = new SqlDataAdapter(command, con);
con.Open();
DataSet ds = new DataSet();
adp.InsertCommand = new SqlCommand("insert into jobs values('15','liuqian','25','100');", con);
adp.Fill(ds, "jobs");
DataTable table = ds.Tables["jobs"];
table.Rows.Add(table.NewRow());
adp.Update(ds, "jobs");
con.Close();
}
/// <summary>
/// 删除数据
/// </summary>
void delete()
{
SqlDataAdapter adp = new SqlDataAdapter(command, con);
con.Open();
DataSet ds = new DataSet();
adp.DeleteCommand = new SqlCommand("delete from employee where job_id = '15';", con);
adp.Fill(ds, "jobs");
DataTable table = ds.Tables["jobs"];
table.Rows[0].Delete();
adp.Update(ds, "jobs");
con.Close();
}
/// <summary>
/// 修改数据库内容
/// </summary>
void update()
{
SqlDataAdapter adp = new SqlDataAdapter(command, con);
con.Open();
DataSet ds = new DataSet();
adp.UpdateCommand = new SqlCommand("update jobs set min_lvl='26' where job_id = '15'; ", con);
adp.Fill(ds, "jobs");
DataTable table = ds.Tables["jobs"];
table.Rows[0][0] = "12";
adp.Update(ds, "jobs");
con.Close();
}
/// <summary>
/// 用SqlDataAdapter对数据库增删改查
/// </summary>
protected void btnAdapter_Click(object sender, EventArgs e)
{
insert();
update();
delete();
}
/// <summary>
/// 用SqlDataAdapter按序查询指定的数据并输出
/// </summary>
protected void btnSelect_Click(object sender, EventArgs e)
{
string command = "select * from employee where job_id>10 order by fname desc ";
SqlDataAdapter adp = new SqlDataAdapter(command, con);
con.Open();
DataSet ds = new DataSet();
adp.Fill(ds, "jobs");
GridView2.DataSource = ds.Tables["jobs"];
GridView2.DataBind();
con.Close();
}
/// <summary>
/// 调用存储过程
/// </summary>
protected void btnProcedure_Click(object sender, EventArgs e)
{
SqlCommand sql = con.CreateCommand();
sql.CommandType = CommandType.StoredProcedure;
sql.CommandText = "aaa";
con.Open();
SqlDataReader reader = sql.ExecuteReader();
if (reader.Read())
{
Response.Write(reader[1]);
}
con.Close();
}
}
}
6. 实验思考题:
1. DataAdapter对象的作用是什么?
SqlDataAdapter是 DataSet和 SQL Server之间的桥接器,用于检索和保存数据。
2. DataReader的特点是什么?
DataReader从数据源中一条一条的读取数据,整个过程都要与数据库保持连接。