2012-08-01 79 views
0

我宣布2个参数为我的存储过程是这样的:存储过程导致错误

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

只是传递价值为以下但在执行的时候,它是导致错误:

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(); 

错误是:

Procedure or function 'paging_select' expects parameter '@startrowindex', which was not supplied.

帮助请。

回答

3

您必须将命令对象传递给SqlDatAdapter

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

PS:始终使用using块来自动处置对象(IDisposable的)。

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

Thanks.its working now。 你能解释为什么它以前不工作吗? – user1544975 2012-08-01 07:15:54

+1

@ user1544975:你有一个'SqlDataAdapter',但是**没有SELECT命令**分配给那个适配器,以便实际上在调用'.Fill()'...时获取数据。 – 2012-08-01 07:25:54