其实DataAdapter执行存储过程还是借助与SQLCommand对象来进行的:
1 private DataSet GetDataSet()
2 {
3
4 SqlConnection conn = DBHelper.GetConn(); //用帮助类得到数据库连接
5 SqlCommand cmd = new SqlCommand("proc_name", conn); //获得SqlCommand对象
6 cmd.CommandType = CommandType.StoredProcedure; //指定SqlCommand对象创建时传入的第一个参数不是SQL语句,而是一个存储过程的名字
7 DataSet ds = new DataSet(); //创建一个DataSet用来安放结果集
8 conn.Open(); //打开数据库
9 SqlDataAdapter dap = new SqlDataAdapter(); //创建SqlDataAdapter对象
10 dap.SelectCommand = cmd; //借助cmd执行存储过程,SqlDataAdapter只需指定SqlCommand的类型(还有DeleteCommand,InsertCommand,UpdateCommand)
11 dap.Fill(ds, "Student"); //填充DataSet
12 conn.Close();
13 return ds;
14 }
SelectCommand 在 SqlDataAdapter.Fill()时调用,UpdateCommand,InsertCommand,DeleteCommand 在SqlDataAdapter.Update() 时调用。