2016-11-29 47 views
0

夥計們,請我已經試過幾乎所有的東西我知道,我也可以實現所有的可能性...失蹤結果查詢從ASP.NET的WebService SQL Server時

會發生什麼事是,當我做出這個選擇通過PROC或commandtext,我的數據讀取器只返回3條記錄,但是我有7個數據庫與這些參數。 如果我將這3個標記爲已讀,併發出新的請購單,則只會將2個發回給我。同樣,如果只有一個,最後一個永遠不會被帶回給我。

這裏是我的代碼

public List<Documentos> GetDocumentosDisponiveisParaMotorista(string motoristaCpf, string veiculoPlaca, 
     long transportadora) 
    { 
     var retorno = new List<Documentos>(); 

     var command = Banco.GetStoredProcCommand("dbo.SelectDocumentos"); 
     AddInParameter(command, "@MotoristaCpf", motoristaCpf); 
     AddInParameter(command, "@VeiculoPlaca", veiculoPlaca.ToUpper()); 
     AddInParameter(command, "@TransportadoraId", transportadora); 

     //var command = Banco.GetSqlStringCommand("SELECT " + 
     //          "TransportadoraId" + 
     //          ", FilialSigla" + 
     //          ", TipoDocumentoId" + 
     //          ", Documento" + 
     //          ", DocumentoSerie" + 
     //          ", MotoristaCpf" + 
     //          ", VeiculoPlaca" + 
     //          ", DocumentoChave" + 
     //          ", DocumentoTransporte" + 
     //          ", TipoDocTransporte" + 
     //          ", NumeroNotaFiscal" + 
     //          ", DestinatarioCpfCnpj" + 
     //          ", LocalEntregaId" + 
     //          ", StatusId" + 
     //          ", Comprovante" + 
     //          ", Transmitido" + 
     //          ", TransmitidoData" + 
     //          ", DataCadastro" + 
     //          ", DataAlteracao " + 
     //          "FROM Documentos WITH(NOLOCK) " + 
     //          "WHERE MotoristaCpf = @MotoristaCpf " + 
     //          "AND VeiculoPlaca = @VeiculoPlaca " + 
     //          "AND TransportadoraId = @TransportadoraId " + 
     //          "AND Transmitido is null "); 

     //command.Parameters.Add(new SqlParameter("@MotoristaCpf", SqlDbType.NVarChar, 11, "MotoristaCpf")); 
     //command.Parameters.Add(new SqlParameter("@VeiculoPlaca", SqlDbType.NVarChar, 7, "VeiculoPlaca")); 
     //command.Parameters.Add(new SqlParameter("@TransportadoraId", SqlDbType.BigInt, 999999999, "TransportadoraId")); 

     //command.Parameters["@MotoristaCpf"].Value = motoristaCpf; 
     //command.Parameters["@VeiculoPlaca"].Value = veiculoPlaca.ToUpper(); 
     //command.Parameters["@TransportadoraId"].Value = transportadora; 
     //command.Connection = Banco.CreateConnection(); 
     //command.Connection.Open(); 
     //command.CommandTimeout = 3600; 

     using (var dataReader = Banco.ExecuteReader(command)) 
     { 
       while (dataReader.Read()) 
       { 
        retorno.Add(dataReader.Read() ? Preencher(dataReader) : null); 
       } 

       dataReader.Close(); 

     } 
     return retorno; 
    } 

我使用SQL Server 2005中這是在asp.net C#WebService的代碼。我的連接是使用連接字符串進行的。我不使用NHibernate,我正在使用Microsoft.Practices.EnterpriseLibrary.Data

+2

您正在推進閱讀器兩次,調用'Read()'兩次。不要這樣做。 – Crowcoder

回答

1

由於Crowcoder mentions in the comments您的主要問題是撥打 dataReader.Read()兩次。看看這related question看看如何使用SqlDataReader.Read()方法。

此外,您無需致電dataReader.Close();,因爲它將在using block的末尾自動處理。

+0

謝謝你們,也是這樣,但是,這給了另一個問題,但艾爾雷迪解決了謝謝 –