2

私のSQLプロシージャは完全に機能しています。しかし、クラスから呼び出すと、データが返されません。そして、私はこのエラーを受け取ります。Column 'Territory_Name' does not belong to table .コードをデバッグしたところ、すべてのパラメーターが正しく渡されていることがわかりましたが、プロシージャーはデータを返しません。誰かがアイデアを持っているなら、私を助けてください。

ここに画像の説明を入力 ここに画像の説明を入力

私のストアドプロシージャは次のとおりです。

ALTER PROCEDURE dbo.SPFilterCRMRequests
(
    /* for Time Span */
    @WeekList varchar(50) = null,
    @MonthList varchar(50) = null,
    @YearList varchar(50) = null,
    @QuaterList varchar(50) = null,
    /* for other specific criteria */
    @PriorityList varchar(50) = null,
    @Division_IdList varchar(50) = null,
    @CRM_Service_Type_IdList varchar(50) = null,
    @CRM_Notation_IdList varchar(50) = null,
    @CRM_State_IdList varchar(50) = null,
    @Service_State_IdList varchar(50) = null,
    @Estimated_Amount float

    /* for Designation 
    @Requester varchar(20),
    @Suggester varchar(20) */
)
AS
    SET NOCOUNT ON 
    /*Declare @date3 DateTime = CONVERT(date,'2/8/2013 5:17:00 PM')*/
    DECLARE @sql nvarchar(4000)

    SELECT @sql='SELECT
    CRMDR.Id as Id,  
    LEFT(GEOTREE.GEONAME,15) as Territory_Name,  
    CRMDR.Request_For_Id as Request_For_Id, 
    DRMST.DRNAME as Request_For_Name, 
    USERSTBM.USERNAME as Requester_Name,
    Division.Div_Name as Division_Name,
    USERSABM.USERNAME as Suggester_Name,
    CRMDR.Estimated_Amount as Estimated_Amount, 
    CRMDR.Actual_Amount as Actual_Amount, 
    CRMDR.Compute_Priority as Compute_Priority, 
    CRMNotation.Notation_Name as CRM_Notation_Name,
    CRMServiceType.ServiceName as CRM_Service_Type_Name, 
    CRMDR.Deadline as Deadline, 
    CRMDR.Executed_Date as Executed_Date, 
    CRMDR.Date_Created as Date_Created, 
    CRMDR.Compute_CRM_State as Compute_CRM_State, 
    CRMDR.Compute_Service_State as Compute_Service_State

    From [CRM].[dbo].[CRM_Doctor_Request] AS CRMDR

    JOIN [ASES].[dbo].[USERS] AS USERSTBM
    ON CRMDR.Requester COLLATE SQL_Latin1_General_CP1_CI_AS = USERSTBM.RID COLLATE SQL_Latin1_General_CP1_CI_AS

    JOIN [ASES].[dbo].[USERS] AS USERSABM
    ON CRMDR.Suggester COLLATE SQL_Latin1_General_CP1_CI_AS = USERSABM.RID COLLATE SQL_Latin1_General_CP1_CI_AS

    JOIN [ASES].[dbo].[GEOTREE] AS GEOTREE
    ON CRMDR.Territory COLLATE SQL_Latin1_General_CP1_CI_AS = GEOTREE.RID COLLATE SQL_Latin1_General_CP1_CI_AS

    JOIN [ASES].[dbo].[DRMST] AS DRMST
    ON CRMDR.Request_For_Id COLLATE SQL_Latin1_General_CP1_CI_AS = DRMST.MDLNO COLLATE SQL_Latin1_General_CP1_CI_AS

    JOIN [CRM].[dbo].[CRM_Request_For_Type] AS CRMReqForType
    ON CRMDR.CRM_Request_For_Type_Id  = CRMReqForType.Id 

    JOIN [CRM].[dbo].[CRM_Notation] AS CRMNotation
    ON CRMDR.CRM_Notation_Id  = CRMNotation.Id 

    JOIN [CRM].[dbo].[CRM_Service_Type] AS CRMServiceType
    ON CRMDR.CRM_Service_Type_Id  = CRMServiceType.Id 

    JOIN [CRM].[dbo].[Division] AS Division
    ON CRMDR.Division_Id  = Division.Id 

    WHERE CRMDR.Is_Deleted=0 '
    If (@MonthList) IS NOT NULL
        SELECT @sql=@sql + ' AND MONTH(CRMDR.Date_Created) in (' + @MonthList + ')  '
    If (@YearList) IS NOT NULL
        SELECT @sql=@sql + ' AND (CRMDR.Date_Created) in (' + @YearList + ') '
    If (@WeekList) IS NOT NULL
BEGIN
    DECLARE @DateCondition varchar(1000)
    DECLARE @ColumnName varchar(50) = 'CRMDR.Date_Created' 
    -- pass the columnname on which condition needs to be aaplied
    EXEC dbo.SPWhereconditionForMultipleWeeks  @WeekList, @ColumnName, @DateCondition OUTPUT
    SELECT @DateCondition
    -- if (LEN(@DateCondition) > 0 )
        SELECT @sql=@sql + ' AND '+ SUBSTRING(@DateCondition, 1, LEN(@DateCondition)-3)
END
If (@QuaterList) IS NOT NULL
BEGIN
    DECLARE @MonthsList varchar(1000)
    EXEC dbo.SPGetMonthsListforMultipleQuaters  @QuaterList, @MonthsList OUTPUT
    SELECT @MonthsList
    -- print @MonthsList
    -- if (LEN(@MonthsList) > 0 )
    SELECT @sql=@sql + ' AND MONTH(CRMDR.Date_Created) in ('
    + SUBSTRING(@MonthsList, 1, LEN(@MonthsList)-1) +')'
END
    If (@PriorityList) IS NOT NULL
        SELECT @sql=@sql + ' AND Priority in (' + @PriorityList + ') '
    If (@Division_IdList) IS NOT NULL
        SELECT @sql=@sql + ' AND Division_Id in (' + @Division_IdList + ') '
    If (@CRM_Service_Type_IdList) IS NOT NULL
        SELECT @sql=@sql + ' AND CRM_Service_Type_Id in (' + @CRM_Service_Type_IdList + ') '
    If (@CRM_Notation_IdList) IS NOT NULL
        SELECT @sql=@sql + ' AND CRM_Notation_Id in (' + @CRM_Notation_IdList + ') '
    If (@Estimated_Amount) IS NOT NULL
        SELECT @sql=@sql + ' AND Estimated_Amount > (@Estimated_Amount) '
    If (@CRM_State_IdList) IS NOT NULL
        SELECT @sql=@sql + ' AND CRM_State_Id in (' + @CRM_State_IdList + ') '
    If (@Service_State_IdList) IS NOT NULL
        SELECT @sql=@sql + ' AND Service_State_Id in (' + @Service_State_IdList + ') '

    SELECT @sql=@sql + ' ORDER BY CRMDR.Id DESC '
    --print @sql

    EXEC sp_executesql @sql, N' @MonthList varchar(50), 
    @YearList varchar(50), 
    @QuaterList varchar(50),  
    @PriorityList varchar(50), 
    @Division_IdList varchar(50), 
    @CRM_Service_Type_IdList varchar(50), 
    @CRM_Notation_IdList varchar(50), 
    @Estimated_Amount float, 
    @CRM_State_IdList varchar(50), 
    @Service_State_IdList varchar(50) ', 
         @MonthList, 
         @YearList, 
         @QuaterList, 
         @PriorityList, 
         @Division_IdList, 
         @CRM_Service_Type_IdList, 
         @CRM_Notation_IdList, 
         @Estimated_Amount, 
         @CRM_State_IdList, 
         @Service_State_IdList
RETURN

プロシージャを呼び出す私のクラス:

public static List<CRM_Doctor_Request> FilterCRM_Doctor_Request_Details(string WeekList, string MonthList, string YearList, string QuaterList, string PriorityList, string Division_IdList,  string CRM_Service_Type_IdList, string CRM_Notation_IdList, string CRM_State_IdList, string Service_State_IdList, float Estimated_Amount) 
        {
            string proc = "SPFilterCRMRequests";

            List<SqlParameter> arrParam = new List<SqlParameter>();
            SqlParameter pWeekList = new SqlParameter("@WeekList", SqlDbType.VarChar);
            SqlParameter pMonthList = new SqlParameter("@MonthList", SqlDbType.VarChar);
            SqlParameter pYearList = new SqlParameter("@YearList", SqlDbType.VarChar);
            SqlParameter pQuaterList = new SqlParameter("@QuaterList", SqlDbType.VarChar);
            SqlParameter pPriorityList = new SqlParameter("@PriorityList", SqlDbType.VarChar);
            SqlParameter pDivision_IdList = new SqlParameter("@Division_IdList", SqlDbType.VarChar);
            SqlParameter pCRM_Service_Type_IdList = new SqlParameter("@CRM_Service_Type_IdList", SqlDbType.VarChar);
            SqlParameter pCRM_Notation_IdList = new SqlParameter("@CRM_Notation_IdList", SqlDbType.VarChar);
            SqlParameter pCRM_State_IdList = new SqlParameter("@CRM_State_IdList", SqlDbType.VarChar);
            SqlParameter pService_State_IdList = new SqlParameter("@Service_State_IdList", SqlDbType.VarChar);
            SqlParameter pEstimated_Amount = new SqlParameter("@Estimated_Amount", SqlDbType.Float);

            pWeekList.Value = WeekList;
            pMonthList.Value = MonthList;
            pYearList.Value = YearList;
            pQuaterList.Value = QuaterList;
            pPriorityList.Value = PriorityList;
            pDivision_IdList.Value = Division_IdList;
            pCRM_Service_Type_IdList.Value = CRM_Service_Type_IdList;
            pCRM_Notation_IdList.Value = CRM_Notation_IdList;
            pCRM_State_IdList.Value = CRM_State_IdList;
            pService_State_IdList.Value = Service_State_IdList;
            pEstimated_Amount.Value = Estimated_Amount;

            arrParam.Add(pWeekList);
            arrParam.Add(pMonthList);
            arrParam.Add(pYearList);
            arrParam.Add(pQuaterList);
            arrParam.Add(pPriorityList);
            arrParam.Add(pDivision_IdList);
            arrParam.Add(pCRM_Service_Type_IdList);
            arrParam.Add(pCRM_Notation_IdList);
            arrParam.Add(pCRM_State_IdList);
            arrParam.Add(pService_State_IdList);
            arrParam.Add(pEstimated_Amount);

            DataTable table = DataProvider.SelectStoreProcedure(proc, arrParam);

            List<CRM_Doctor_Request> ListCRM_Doctor_Request = new List<CRM_Doctor_Request>();

            foreach (DataRow row in table.Rows)
            {
                CRM_Doctor_Request CRM_Doctor_RequestObj = new CRM_Doctor_Request();

                CRM_Doctor_RequestObj.Territory_Name = Convert.ToString(row["Territory_Name"]);
                CRM_Doctor_RequestObj.Request_For_Id = Convert.ToString(row["Request_For_Id"]);
                CRM_Doctor_RequestObj.Request_For_Name = Convert.ToString(row["Request_For_Name"]);
                CRM_Doctor_RequestObj.Requester_Name = Convert.ToString(row["Requester_Name"]);
                CRM_Doctor_RequestObj.Division_Name = Convert.ToString(row["Division_Name"]);
                CRM_Doctor_RequestObj.Suggester_Name = Convert.ToString(row["Suggester_Name"]);
                CRM_Doctor_RequestObj.Id = Convert.ToInt32(row["Id"]);    
                CRM_Doctor_RequestObj.Compute_Priority = Convert.ToString(row["Compute_Priority"]);
                CRM_Doctor_RequestObj.CRM_Notation_Name = Convert.ToString(row["CRM_Notation_Name"]);
                CRM_Doctor_RequestObj.CRM_Service_Type_Name = Convert.ToString(row["CRM_Service_Type_Name"]);                    
                CRM_Doctor_RequestObj.Compute_CRM_State = Convert.ToString(row["Compute_CRM_State"]);
                CRM_Doctor_RequestObj.Compute_Service_State = Convert.ToString(row["Compute_Service_State"]);

                ListCRM_Doctor_Request.Add(CRM_Doctor_RequestObj);
            }
            return ListCRM_Doctor_Request;
        }

DataProvider.SelectStoreProcedure のコード:

class DataProvider
    {
        public static string connectionString = ConfigurationManager.ConnectionStrings["connect_str"].ConnectionString;

        public static DataTable SelectStoreProcedure(string ProcName, List<SqlParameter> ParaArr)
        {
                DataTable data = new DataTable();
                SqlConnection cn = new SqlConnection(connectionString);
                cn.Open();
                SqlCommand cmd = new SqlCommand(ProcName, cn);
                foreach (SqlParameter para in ParaArr)
                {
                    cmd.Parameters.Add(para);
                }
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(data);
                cn.Close();
                return data;
        }
    }
4

2 に答える 2

1

これはとても乱雑に見えます。複数のクエリを実行しているため、正しいクエリの結果を Datatable に入力するか、Datatable にDataSet入力する必要があると思います。take the 0th Table

また、接続オブジェクトParameters.AddWithValue()を使用してコードを半分に減らすこともできます。using

//Your method
DataSet data = new DataSet(); //Note Dataset here ****
string connectionString = ConfigurationManager.ConnectionStrings["connect_str"].ConnectionString;

using(SqlConnection cn = new SqlConnection(connectionString))
{
    string proc = "SPFilterCRMRequests";
    SqlCommand cmd = new SqlCommand(proc, cn);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@WeekList", WeekList);
    cmd.Parameters.AddWithValue("@MonthList", pMonthList);
    cmd.Parameters.AddWithValue("@YearList", pYearList);
    cmd.Parameters.AddWithValue("@QuaterList", pQuaterList);
    cmd.Parameters.AddWithValue("@PriorityList", pPriorityList);
    cmd.Parameters.AddWithValue("@Division_IdList", pDivision_IdList);
    cmd.Parameters.AddWithValue("@CRM_Service_Type_IdList", pCRM_Service_Type_IdList);
    cmd.Parameters.AddWithValue("@CRM_Notation_IdList", pCRM_Notation_IdList);
    cmd.Parameters.AddWithValue("@CRM_State_IdList", pCRM_State_IdList);
    cmd.Parameters.AddWithValue("@Service_State_IdList", pService_State_IdList);
    cmd.Parameters.AddWithValue("@Estimated_Amount", pEstimated_Amount);


    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cn.Open();
    da.Fill(data);
}

List<CRM_Doctor_Request> ListCRM_Doctor_Request = 
                         new List<CRM_Doctor_Request>();

foreach (DataRow row in data.Tables[0].Rows) //**** Note 0th table here ******
{
 //rest of the code
}
//...
于 2013-02-26T13:25:28.813 に答える
0

あなたの問題は、テーブルを埋めようとしていることです(スキーマが定義されていません)

DataTable data = new DataTable();に変更DataSet data = new DataSet();

そして DataTable table = dataDataTable table = data.Tables[0]

スキーマが自動的に生成され、すべてが機能するはずです。

**編集**

このセットアップからクエリを構築してみてください...

このストアド プロシージャを定義する

CREATE PROCEDURE [dbo].[TestProc1] 
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @sql NVARCHAR(MAX) = 'SELECT 1 x UNION ALL SELECT 2'

    EXEC sp_executesql @sql
END

このコードだけを含むテスト アプリを作成する

class Program
{
    public const string ConnectionString = @"???";

    static void Main(string[] args)
    {
        var t = Execute("TestProc1");

        Trace.Assert(t.Rows.Count == 2);
    }

    private static DataTable Execute(string sql)
    {
        using (var connection = new SqlConnection(ConnectionString))
        {
            connection.Open();

            using (var command = new SqlCommand(sql, connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                var da = new SqlDataAdapter(command);

                var ds = new DataSet();
                da.Fill(ds);

                return ds.Tables[0];
            }
        }
    }
}

接続文字列は接続文字列です。

それは機能し、機能的にはあなたがしていることと同等です。それが機能したら、特定の状況に合わせて構築してみてください

于 2013-02-26T13:22:07.180 に答える