简体   繁体   English

存储过程导致错误

[英]Stored procedure causing error

I declared 2 parameters for my stored procedure like this: 我为存储过程声明了2个参数,如下所示:

ALTER procedure [dbo].[paging_select]
    @startrowindex int,
    @maximumrows int
as
begin
    select username,firstname,lastname from crudtable ;
end

Simply passing the value as following but when executing it, it is causing an error: 只需按照以下方式传递值,但是在执行它时会导致错误:

SqlConnection con = new SqlConnection(getconnectionstring());
con.Open();

DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter("paging_select", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@startrowindex", 1);
cmd.Parameters.AddWithValue("@maximumrows", 3);
//  cmd.Parameters.AddWithValue("@totalrows", 1);

cmd.Connection = con;
sda.Fill(dt);  
sda.Dispose();
gridview.DataSource = dt;
gridview.DataBind();
con.Close();

Error was: 错误是:

Procedure or function 'paging_select' expects parameter '@startrowindex', which was not supplied. 过程或函数'paging_select'需要未提供的参数'@startrowindex'。

Help Please. 请帮助。

You have to pass command object to the SqlDatAdapter . 您必须将命令对象传递给SqlDatAdapter

SqlCommand cmd = new SqlCommand();
cmd.CommandText="paging_select";
cmd.Connection=con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);

PS: Always use using block to dispose objects automatically ( IDisposable ). PS:始终使用using块自动放置对象( IDisposable )。

using(SqlConnection cn=new SqlConnection())
{
  //
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM