当前位置: 代码迷 >> ASP.NET >> sqldatasource控件给存储过程传参有关问题
  详细解决方案

sqldatasource控件给存储过程传参有关问题

热度:1091   发布时间:2013-02-25 00:00:00.0
sqldatasource控件给存储过程传参问题
前台代码:
  string kssj = TextBox2.Text;
  string jssj = TextBox4.Text;
  SqlDataSource1.ConnectionString = "Data Source=LJ-PC\\SQLEXPRESS;Initial Catalog=StudyRoom;Integrated Security=True";
  SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
  SqlDataSource1.SelectCommand = "datasum_proc";
  SqlDataSource1.SelectParameters.Add("@p_kssj", TypeCode.String, kssj);
  SqlDataSource1.SelectParameters.Add("@p_jssj", TypeCode.String, jssj);
  GridView1.DataSource = SqlDataSource1;
  SqlDataSource1.DataBind();
  GridView1.DataBind();

数据库存储过程:
create proc datasum_proc
@p_kssj varchar(10),
@p_jssj varchar(10)

as

select * from table where kssj between @p_kssj and @p_jssj

return 0



运行vs2005下该页面的时候 一直提示 未提供参数 @p_kssj

------解决方案--------------------------------------------------------

SqlDataSource1.DataBind();
GridView1.DataSource = SqlDataSource1;

换下顺序

或者
SqlConnection connection = new SqlConnection("Data Source=LJ-PC\\SQLEXPRESS;Initial Catalog=StudyRoom;Integrated Security=True");
connection.Open();
SqlCommand command = new SqlCommand("datasum_proc", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@p_kssj", kssj);
command.Parameters.AddWithValue("@p_jssj", jssj);
SqlDataReader reader = command.ExecuteReader();
GridView1.DataSource = reader ;
GridView1.DataBind();