2017-05-09 24 views
-2

附近有语法错误','异常,当应用程序设置为运行出现。

在SQL Server Management Studio查询运行良好。但它在应用程序设置为运行时引发异常。

存储过程:

SET ANSI_NULLS ON; 
SET QUOTED_IDENTIFIER ON; 
GO 

ALTER PROCEDURE dbo.[ USP_GET_MONTH_WISE_PENDING_DETAILS] 
    @MONTH INT = 4, 
    @YEAR INT = 2017 
AS 
BEGIN 
    SET NOCOUNT ON; 

    SELECT 
     P.PbsName, PC.CauseName, 
     dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.DayFrom) + N'/' + dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.MonthFrom) + N'/' + 
       dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.YearFrom) + N'থেকে' + dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.DayTo) + N'/' + 
       dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.MonthTo) + N'/' + dbo.F_CONVERT_ENG_NUMBER_TO_BNG(PCI.YearTo) 'Date', 
     PCI.Remarks 
    FROM  
     Pbs P 
    INNER JOIN 
     ProbableConnectionInfo PCI ON PCI.PbsId = P.PbsId 
    INNER JOIN 
     ConnectionPendingCause CPC ON PCI.Id = CPC.ProbableConnectionId 
    INNER JOIN 
     PendingCause PC ON CPC.PendingCauseId = PC.Id 
    WHERE 
     PCI.MONTH = @MONTH AND 
     YEAR = @YEAR AND 
     CPC.PendingNumber > 0; 

    SELECT 
     dbo.F_CONVERT_ENG_NUMBER_TO_BNG(SUM(IC.Pending)) 'PendingApplication', 
     dbo.F_CONVERT_ENG_NUMBER_TO_BNG(SUM(IC.loadNeed)) 'AppliedLoadAmount' 
    FROM  
     IndustrialConnection IC 
    INNER JOIN 
     ProbableConnectionInfo PCI ON IC.ProbableConnectionId = PCI.Id 
    WHERE 
     PCI.MONTH = @MONTH AND YEAR = @YEAR 
    GROUP BY 
     PCI.PbsId; 

    SET NOCOUNT OFF; 
END; 

,这里是哪里出了问题被显示出来:

public static DataTable GetDataByStoredProcedure(string spName, List<SqlParameter> pars = null) 
{ 
    sqlConnection.Open(); 

    var sqlCommand = new SqlCommand(spName, sqlConnection); 
    sqlCommand.CommandType = CommandType.StoredProcedure; 

    if (pars != null) 
    { 
     sqlCommand.Parameters.AddRange(pars.ToArray()); 
    } 

    var dataReader = sqlCommand.ExecuteReader(); 
    var dt = new DataTable("Command"); 
    dt.Load(dataReader); 

    //sqlConnection.Close(); 
    return dt; 
} 

这里是我使用的方法:

public void ShowPendingIndustrialInfo(string exportType, int year, int month) 
{ 
    List<SqlParameter> pars = new List<SqlParameter>(); 

    SqlParameter p1 = new SqlParameter("@month", SqlDbType.Int); 
    p1.Value = month; 

    SqlParameter p2 = new SqlParameter("@year", SqlDbType.Int); 
    p2.Value = year; 

    pars.Add(p1); 
    pars.Add(p2); 

    var data = SPService.GetDataByStoredProcedure("USP_GET_MONTH_WISE_PENDING_DETAILS",pars); 
    ReportHelper.ShowReport(data, exportType, "rptPendingDetails.rpt"); 
} 

回答

2

当你建立一个用于运行存储过程的SqlCommand,您需要将CommandType属性设置为CommandType.StoredProcedure,否则,该属性的默认值是Text,并且引擎试图解析您的命令名称,如if是一个文字sql命令。当然,名称month_wise_pending_details不是有效的sql命令文本。

var sqlCommand = new SqlCommand(spName, sqlConnection); 
    sqlCommand.CommandType = CommandType.StoredProcedure; 
    var dataReader = sqlCommand.ExecuteReader(); 

我也应该在代码中提出警告。你有一个全局连接对象,你不会在使用它之后进行处理。这是一个不好的做法,因为ADO.NET实现Connection Pooling以极高的效率不再需要保持这些类型的对象总是打开

public class SPService 
{ 
    private static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; 

    public static DataTable GetDataByStoredProcedure(string spName) 
    { 
     using(SqlConnection sqlConnection = new SqlConnection(connectionString)) 
     using(SqlCommand sqlCommand = new SqlCommand(spName, sqlConnection)) 
     { 
      sqlConnection.Open(); 
      sqlCommand.CommandType = CommandType.StoredProcedure; 
      using(SqlDataReader dataReader = sqlCommand.ExecuteReader()) 
      { 
       var dt = new DataTable("Command"); 
       dt.Load(dataReader); 
       return dt; 
      } 
     } 
    } 
} 

编辑
如果你想传递参数给你的存储过程,那么你需要一些修改代码:

的方法应该接受List<SqlParameter>作为第二个参数,这个列表添加到收藏SqlCommand.Parameters

public static DataTable GetDataByStoredProcedure(string spName, List<SqlParameter> pars = null) 
{ 
    using(SqlConnection sqlConnection = new SqlConnection(connectionString)) 
    using(SqlCommand sqlCommand = new SqlCommand(spName, sqlConnection)) 
    { 
     sqlConnection.Open(); 
     sqlCommand.CommandType = CommandType.StoredProcedure; 
     if(pars != null) sqlCommand.Parameters.AddRange(pars.ToArray()); 
     using(SqlDataReader dataReader = sqlCommand.ExecuteReader()) 
     { 
      var dt = new DataTable("Command"); 
      dt.Load(dataReader); 
      return dt; 
     } 
    } 
} 

而且调用代码修改这些参数传递

public void ShowPendingIndustrialInfo(string exportType, int year, int month) 
{ 
    List<SqlParameter> pars = new List<SqlParameter>(); 
    SqlParameter p1 = new SqlParameter("@month", SqlDbType.Int); 
    p1.Value = month; 
    SqlParameter p2 = new SqlParameter("@year", SqlDbType.Int); 
    p2.Value = year; 
    pars.Add(p1); 
    pars.Add(p2); 
    var data = SPService.GetDataByStoredProcedure("USP_GET_MONTH_WISE_PENDING_DETAILS", pars); 
    ReportHelper.ShowReport(data, exportType, "rptPendingDetails.rpt"); 
} 

最后编辑
关于未找到存储过程中的错误:如果你仔细看ALTER语句,你会发现在名称前一个空格的程序。这是错误的persinstence的也都与以前固定

ALTER PROCEDURE dbo.[ USP_GET_MONTH_WISE_PENDING_DETAILS] 
        ^
+0

我注意到你已经改变了sp的名字。现在它读取为* USP_GET_MONTH_WISE_PENDING_DETAILS *。哪一个是正确的?你可以用调试器检查传递给你的方法的参数的值是什么? – Steve

+0

参数作为“USP_GET_MONTH_WISE_PENDING_DETAILS4,2017”(月,年)传递给我认为正确的方法。但程序没有找到。 @Steve –

+0

如果你的存储过程被命名为USP_GET_MONTH_WISE_PENDING_DETAILS但你通过USP_GET_MONTH_WISE_PENDING_DETAILS,4,2017然后难怪如果数据库找不到名称 – Steve

0

RU,

在代码中添加下面的命令通知C#的SqlCommand的是存储过程后的原因。

sqlCommand.CommandType = CommandType.StoredProcedure; 

IE:

namespace PBSM.DBGateway 
{ 
public class SPService 
{ 
    private static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString; 
    private static SqlConnection sqlConnection = new SqlConnection(connectionString); 

    public static DataTable GetDataByStoredProcedure(string spName) 
    { 
     sqlConnection.Open(); 
     var sqlCommand = new SqlCommand(spName, sqlConnection); 
     **sqlCommand.CommandType = CommandType.StoredProcedure;** 
     var dataReader = sqlCommand.ExecuteReader(); 
     var dt = new DataTable("Command"); 
     dt.Load(dataReader); 
     sqlConnection.Close(); 
     return dt; 
    } 
} 

}

请记住我的答案回应,如果它帮助你。问候。

相关问题