2013-07-04 55 views
0

我已經創建了SQL Server 2008的過程:從C#中調用存儲過程不工作

CREATE PROCEDURE USP_VISUALIZAR_PIS_PROCESSO_IMPORTACAO_DETALHE_HISTORICO 
@DATA_HISTORICO DATETIME 
, @SIGLA_ESTADO VARCHAR(2) 
, @ANO_MES INT 
, @SEMANA_MES INT 
, @CODIGO_LINHA_FICTICIA INT 
AS 
BEGIN 

SELECT P.PROD_CD_PRODUTO 
     , P.PROD_DS_PRODUTO 
     , HIST.PIDH_DT_NOTA_FISCAL 
     , HIST.PIDH_NR_ORDEM 
     , HIST.PIDH_NR_ITEM 
     , HIST.PIDH_CD_PROCESSO 
     , HIST.PIDH_DT_PREV_BOOKING 
     , HIST.PIDH_DT_CHEGADA_PORTO 
     , HIST.PIDH_DT_PREVISAO_EMBARQUE_ORDEM 
     , HIST.PIDH_DT_LIBERACAO 
     , ISNULL(HIST.PIDH_DS_CANAL, '-') AS PIDH_DS_CANAL 
     , HIST.PIDH_CD_REFERENCIA 
     , HIST.PIDH_CD_REMESSA 
     , HIST.PIDH_IN_FATURAS_IMPORTACAO 
     , HIST.PIDH_NM_FORNECEDOR 
     , CASE WHEN HIST.PIDH_DT_NOTA_FISCAL IS NOT NULL THEN 'Sim' ELSE 'NãO' END AS INDICADOR_RECEBIDO 
     , SUM(HIST.PIDH_QN_FATURA) AS PIDH_QN_FATURA 
     , MAX(HIST.PIDH_QN_ORDEM) AS PIDH_QN_ORDEM 
     , MAX(HIST.PIDH_QN_DIAS_DEMURRAGE) AS PIDH_QN_DIAS_DEMURRAGE 
     , CASE WHEN HIST.PIDH_DT_LIBERACAO IS NOT NULL THEN 1 ELSE 0 END AS LIBERADO 
     , HIST.PIDH_DT_HISTORICO 
     , HIST.DDT_NR_SEMANA_MES 
     , HIST.DDT_NR_ANOMES 
    FROM PROCESSO_IMPORTACAO_DETALHE_HISTORICO HIST 
     INNER JOIN ESTADO E ON E.ESTA_CD_ESTADO = HIST.ESTA_CD_ESTADO 
     INNER JOIN PRODUTO P ON P.PROD_CD_PRODUTO = HIST.PROD_CD_PRODUTO 
     INNER JOIN LINHA L ON L.LINH_CD_LINHA = P.LINH_CD_LINHA 
     INNER JOIN LINHA_FICTICIA LF ON LF.LIFI_CD_LINHA_FICTICIA = L.LIFI_CD_LINHA_FICTICIA 
     INNER JOIN VOLTAGEM V ON V.VOLT_CD_VOLTAGEM = P.VOLT_CD_VOLTAGEM 
     INNER JOIN PRODUTO_COMERCIAL PC ON PC.PRCO_CD_PRODUTO_COMERCIAL = P.PRCO_CD_PRODUTO_COMERCIAL 
WHERE HIST.PIDH_DT_HISTORICO = @DATA_HISTORICO 
    GROUP BY P.PROD_CD_PRODUTO 
     , P.PROD_DS_PRODUTO 
     , HIST.PIDH_DT_NOTA_FISCAL 
     , HIST.PIDH_NR_ORDEM 
     , HIST.PIDH_NR_ITEM 
     , HIST.PIDH_CD_PROCESSO 
     , HIST.PIDH_DT_PREV_BOOKING 
     , HIST.PIDH_DT_CHEGADA_PORTO 
     , HIST.PIDH_DT_PREVISAO_EMBARQUE_ORDEM 
     , HIST.PIDH_DT_LIBERACAO 
     , HIST.PIDH_DS_CANAL 
     , HIST.PIDH_CD_REFERENCIA 
     , HIST.PIDH_CD_REMESSA 
     , HIST.PIDH_IN_FATURAS_IMPORTACAO 
     , HIST.PIDH_NM_FORNECEDOR 
     , HIST.PIDH_DT_HISTORICO 
     , HIST.DDT_NR_SEMANA_MES 
     , HIST.DDT_NR_ANOMES 
END 

這個程序工作正常,當我從SQL Server Management Studio中調用,但是當我從C#調用它doesn沒有工作。我需要這個程序的輸出,但我沒有成功。

的C#代碼是:

object[] o = new object[5]; 
o[0] = dataHistorico; 
     o[1] = filtro.SiglaEstado; 
     o[2] = filtro.AnoMes; 
     o[3] = filtro.SemanaMes; 
     o[4] = linhaFiciticia; 

string query = "exec USP_VISUALIZAR_PIS_PROCESSO_IMPORTACAO_DETALHE_HISTORICO @DATA_HISTORICO = {0}, @SIGLA_ESTADO = {1}, @ANO_MES = {2}, @SEMANA_MES = {3}, @CODIGO_LINHA_FICTICIA = {4}"; 
var r = db.Database.ExecuteSqlCommand(query, o); 

這段代碼的輸出總是-1,但是當我從SQL Server中執行它返回超過10000的記錄。

怎麼了?有人可以幫我解決這個問題嗎?

UPDATE

這裏是解決方案:

 string query = "exec USP_VISUALIZAR_PIS_PROCESSO_IMPORTACAO_DETALHE_HISTORICO"; 
     string connectionString = System.Configuration.ConfigurationSettings.AppSettings["StringConexao"].ToString(); 

     SqlConnection conn = new SqlConnection(connectionString);    
     SqlCommand command = new SqlCommand(query + " @DATA_HISTORICO = '" + dataHistorico.ToString("yyyy-MM-dd") + "', @SIGLA_ESTADO = " + filtro.SiglaEstado + ", @ANO_MES = " + filtro.AnoMes + ", @SEMANA_MES = " + filtro.SemanaMes + ", @CODIGO_LINHA_FICTICIA = " + linhaFiciticia, conn); 

     command.Connection.Open(); 
     SqlDataAdapter sda = new SqlDataAdapter(command); 
     command.Connection.Close(); 

     DataTable dt = new DataTable(); 
     sda.Fill(dt); 

     conn.Close(); 

     command.Connection.Close(); 

     foreach (DataRow row in dt.Rows) 
     { 
      ProcessoImportacaoDetalhePISVO pi = new ProcessoImportacaoDetalhePISVO(); 

      string processo = row["PIDH_CD_PROCESSO"].ToString(); 
      string referencia = row["PIDH_CD_REFERENCIA"].ToString(); 

      pi.Processo = !String.IsNullOrEmpty(processo) ? processo : referencia; 
      lista.Add(pi); 
     } 

謝謝你們的幫助!

+1

db.Database.ExecuteSqlCommand是做什麼的? – Arran

+0

您是否嘗試過單獨傳遞參數? db.Database.ExecuteSqlCommand(query,dataHistorico,filtro.SiglaEstado,...) – wudzik

+0

如果使用存儲過程,爲什麼不使用SqlCommand和SqlDataAdapter? – Tim

回答

2

你應該在存儲過程中使用SqlCommand。嘗試這樣的事情。

string connectionString = "your connection string"; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 
      connection.Open(); 
      using (SqlCommand command = new SqlCommand("USP_VISUALIZAR_PIS_PROCESSO_IMPORTACAO_DETALHE_HISTORICO", connection)) 
      { 
       command.CommandTimeout = 0; 
       command.CommandType = System.Data.CommandType.StoredProcedure; 

       SqlParameter p1 = command.Parameters.Add("@DATA_HISTORICO", SqlDbType.DateTime); 
       p1.Direction = ParameterDirection.Input; 
       p1.Value = dataHistorico; 

       SqlParameter p2 = command.Parameters.Add("@SIGLA_ESTADO", SqlDbType.VarChar); 
       p2.Direction = ParameterDirection.Input; 
       p2.Value = filtro.SiglaEstado; 

       //.... continue for rest of parameters 

       using (SqlDataReader reader = command.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 

         //Read your values into some data structure 
        } 
       } 
      } 
     } 
+0

謝謝你的幫助。我使用了這個想法,但我以不同的方式做到了。我會用解決方案更新我的帖子。 –

0

問題是,ExecuteSqlCommand只是返回SQL Server返回的內容。如果你想返回行數,那麼你可以通過選擇rowcount到SP中的一個局部變量並從存儲過程中返回它來完成。

如果您的目標是從SP獲取記錄,那麼您需要探索數據庫SqlQuery方法,並查看哪些重載適用於您的情況。

0

ExecuteSqlCommand用於執行「命令」 - 在ADO.NET中它不返回結果集(稱爲「查詢」)。由於它看起來像使用實體框架,因此您可以在設計器中對其進行建模或使用SqlQuery方法。