Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用存储过程检索数据_C#_Sql_Sql Server_Stored Procedures_Ado.net - Fatal编程技术网

C# 使用存储过程检索数据

C# 使用存储过程检索数据,c#,sql,sql-server,stored-procedures,ado.net,C#,Sql,Sql Server,Stored Procedures,Ado.net,我编写了以下C#代码来从数据库中获取数据: public static DataTable GetDetails(string startDate, string endDate, char isSpam, string Name) { string getDetilasQuery = @"sp_getDetails"; SqlParameter[] objSqlParameter = { new SqlParameter("@isDeleted",'n'),

我编写了以下C#代码来从数据库中获取数据:

public static DataTable GetDetails(string startDate, string endDate, char isSpam, string Name)
{
    string getDetilasQuery = @"sp_getDetails";

    SqlParameter[] objSqlParameter = {  new SqlParameter("@isDeleted",'n'),
                                        new SqlParameter("@startDate",startDate),
                                        new SqlParameter("@endDate",endDate),
                                        new SqlParameter("@isSpam",isSpam),
                                        new SqlParameter("@Name",Name)
                                     };

    return DL.decryptDataSet(SqlHelper.ExecuteDataset(strConnectionString, CommandType.Text, getDetilasQuery, objSqlParameter)).Tables[0];
}
我的存储过程如下所示:

CREATE PROCEDURE [dbo].[sp_getDetails]
    (@isDeleted CHAR(1),
     @startDate DATETIME,
     @endDate DATETIME,
     @isSpam CHAR(1),
     @Name VARCHAR(MAX)) 
 As
 BEGIN 
     SELECT 
         col1, col2.... 
     FROM
         tbl_getData 
     WHERE
         added BETWEEN @startDate AND @endDate 
         AND isDeleted = @isDeleted
END
但每当我尝试使用以下代码和存储过程检索数据时,都会出现一个错误

存储过程需要未提供的参数@isDeleted

但是,我使用代码中的
SqlParameter[]
传递了一个参数
@isDeleted


有人能告诉我我是否丢失了一些东西,或者代码或存储过程中有任何更改吗?

正如@Jeroen更改CommandType.Text到CommandType.StoredProcedure所建议的那样。

CommandType.Text
更改为
CommandType.StoredProcedure
。我不得不说这是最烦人的错误消息之一。我的意思是,从文本中可以清楚地看到ado.net“知道”这是一个存储过程,问题不是缺少参数而是错误的
commandType
。这应该是另一条错误消息。@Jeroenmoster感谢您找出我的错误!:-)旁注:存储过程不应使用
sp
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用
sp.
并使用其他东西作为前缀,或者根本不使用前缀@ZoharPeled:不——ADO.NET不知道您的命令文本中有什么。SQL Server处理“查询”
sp_getDetails
,将其解释为
EXEC sp_getDetails
,而不使用
EXEC
,然后给出缺少参数的错误。在ManagementStudio中也会出现同样的错误。(当然,从技术上讲,当SQL Server准确返回此错误时,ADO.NET可能会有特殊的处理,但在所有情况下都很难做到这一点,只是为了改善用户体验。)这正是
CommandType.StoredProcedure
首先存在的原因。