2013-10-22 119 views
3

我有一个方法可以获得sql连接和连接。创建一个新的命令,添加一些参数,然后运行一个适配器来填充DataTable。但是,我收到错误,说我没有提供参数 - 即使我明确了。使用参数运行存储过程

你认为在这里出了什么问题?

public static DataTable getStatsPaged(int currentPage, int pageSize, int year, int month, int day, int logType, int itemid, string stat) 
{ 
    using (SqlConnection connection = sqldb.getSqlConnection("db2")) 
    { 
     connection.Open(); 

     using (SqlCommand command = new SqlCommand("stp_FidusTrak_"+stat+"_paged", connection)) 
     { 
      command.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, ((System.Byte)(0)), ((System.Byte)(0)), "", DataRowVersion.Current, null)); 
      command.Parameters.Add(new SqlParameter("@CurrentPage", currentPage)); 
      command.Parameters.Add(new SqlParameter("@PageSize", pageSize)); 
      command.Parameters.Add(new SqlParameter("@Year", year)); 
      command.Parameters.Add(new SqlParameter("@Month", month)); 
      command.Parameters.Add(new SqlParameter("@Day", day)); 
      //error is this logType 
      //Procedure or function 'stp_FidusTrak_SearchParameterSelect_paged' expects parameter '@LogType', which was not supplied. 
      command.Parameters.Add(new SqlParameter("@LogType", -10)); 
      command.Parameters.Add(new SqlParameter("@itemId", itemid)); 
      command.Parameters.Add(new SqlParameter("@TotalRecords", SqlDbType.Int, 4, ParameterDirection.Output, false, ((System.Byte)(0)), ((System.Byte)(0)), "", DataRowVersion.Current, null)); 
      command.Parameters.Add(new SqlParameter("@FirstDate", SqlDbType.SmallDateTime, 4, ParameterDirection.Output, false, ((System.Byte)(0)), ((System.Byte)(0)), "", DataRowVersion.Current, null)); 

      using (SqlDataAdapter adapter = new SqlDataAdapter()) 
      { 
       adapter.SelectCommand = command; 
       DataTable table = new DataTable(); 
       adapter.Fill(table); 

       return table; 
      } 
     } 
    } 
} 

将正常传递中作为一个变量,但即使我只是把它写在-10,还在说不能提供..

任何人有一个想法?也许我使用的东西是错的?

谢谢。

+0

你的SQL看起来像什么? – Liam

+0

**存储过程**是否有参数? – DGibbs

+0

如果可能的话也分享你的SP。 – Jay

回答

2

您还没有告诉ADO.NET这是您拨打的存储过程

加入这一行:

using (SqlCommand command = new SqlCommand("stp_FidusTrak_"+stat+"_paged", connection)) 
{ 
    // tell it that it's a stored procedure 
    command.CommandType = CommandType.StoredProcedure; 
0

检查你所传递的存储过程,并提到StoredProcedure类型的命令对象的LogType参数类型,因为它需要默认Text

尝试下面一行: -

command.Parameters.Add(new SqlParameter("@LogType", "-10")); 
1

在代码中使用SP时必须插入此行。

command.CommandType = CommandType.StoredProcedure;