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();
}
///
/// 插入数据
///
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();
}
///
/// 删除数据
///
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();
}
///
/// 修改数据库内容
///
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();
}
///
/// 用SqlDataAdapter对数据库增删改查
///
protected void btnAdapter_Click(object sender, EventArgs e)
{
insert();
update();
delete();
}
///
/// 用SqlDataAdapter按序查询指定的数据并输出
///
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();
}
///
/// 调用存储过程
///
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. 实验思考题: