C# 为什么说$exception ;(';,';";附近的语法不正确) ;System.Exception{System.Data.SqlClient.SqlException}

C# 为什么说$exception ;(';,';";附近的语法不正确) ;System.Exception{System.Data.SqlClient.SqlException},c#,sql,sql-server,asp.net-mvc,entity-framework-6,C#,Sql,Sql Server,Asp.net Mvc,Entity Framework 6,当应用程序设置为运行时,会出现异常 在SQLServerManagementStudio中,查询运行正常。但当应用程序设置为运行时,它会抛出一个异常 存储过程: 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 NOCO

当应用程序设置为运行时,会出现异常

在SQLServerManagementStudio中,查询运行正常。但当应用程序设置为运行时,它会抛出一个异常

存储过程:

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 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");
}
这就是问题所在:

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;
}
publicstaticdatatable getdataystoredprocedure(stringspname,List pars=null)
{
sqlConnection.Open();
var sqlCommand=新的sqlCommand(spName,sqlConnection);
sqlCommand.CommandType=CommandType.StoredProcess;
如果(PAR!=null)
{
sqlCommand.Parameters.AddRange(pars.ToArray());
}
var dataReader=sqlCommand.ExecuteReader();
var dt=新数据表(“命令”);
dt.负载(数据读取器);
//sqlConnection.Close();
返回dt;
}
以下是我使用程序的地方:

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 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");
}
public void ShowPendingIndustrialInfo(字符串导出类型,整数年,整数月)
{
List pars=新列表();
SqlParameter p1=新的SqlParameter(“@month”,SqlDbType.Int);
p1.价值=月份;
SqlParameter p2=新的SqlParameter(“@year”,SqlDbType.Int);
p2.价值=年;
添加部分(p1);
新增部分(p2);
var data=SPService.getDataystoredProcess(“USP\u获得\u月\u明智\u待定\u详细信息”,PAR);
ReportHelper.ShowReport(数据,导出类型,“rptPendingDetails.rpt”);
}

构建SqlCommand以运行存储过程时,需要将CommandType属性设置为CommandType.StoredProcedure,否则,此属性的默认值为Text,引擎将尝试解析命令名,就像解析文字sql命令一样。当然,名称month_wise_pending_details不是有效的sql命令文本

  var sqlCommand = new SqlCommand(spName, sqlConnection);
  sqlCommand.CommandType = CommandType.StoredProcedure;
  var dataReader = sqlCommand.ExecuteReader();
我还应该警告您代码中存在问题。您有一个全局连接对象,并且在使用该对象后不会对其进行处理。这是一种不好的做法,因为ADO.NET实现效率很高,无需始终保持此类对象的打开状态

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;
             }
        }
    }
}
编辑
如果要将参数传递给存储过程,则需要对代码进行一些更改:

该方法应接收一个
列表
作为第二个参数,此列表将添加到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;
         }
    }
}

在代码中添加以下命令,通知c#sqlcommand是一个存储过程

sqlCommand.CommandType = CommandType.StoredProcedure;
即:

}


如果我的回答对你有帮助的话,请注明。问候。

我注意到您更改了sp的名称。现在它的内容是USP\u GET\u MONTH\u WISE\u PENDING\u DETAILS。哪个是正确的?你能用调试器检查一下传递给你的方法的参数值是多少吗?传递给方法的参数是“USP\u GET\u MONTH\u WISE\u PENDING\u details42017”(月,年),我认为这是正确的。但程序没有找到@Steven如果您的存储过程名为USP_GET_MONTH_WISE_PENDING_DETAILS,但您传递了USP_GET_MONTH_WISE_PENDING_DETAILS,42017,那么数据库找不到该名称也就不足为奇了问题出在SP的名称中。您添加了月份和年份,但这使得名称不明确。我添加了代码,向您展示如何传递SP所需的可选参数(@mont和@year)。
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;
    }
}