从ASP.NET WebService查询SQL Server时缺少结果

时间:2016-11-29 13:23:02

标签: c# sql-server web-services sqldatareader

伙计们,我已经尝试了几乎所有我认识的事情以及我可以实现的所有可能性......

当我通过proc或commandtext进行选择时,我的datareader只返回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代码。我的连接是使用connectionstring进行的。我没有使用NHibernate而且我使用Microsoft.Practices.EnterpriseLibrary.Data

1 个答案:

答案 0 :(得分:1)

作为Crowcoder mentions in the comments你的主要问题是打电话     dataReader.Read()两次。请查看此related question,了解如何使用SqlDataReader.Read()方法。

此外,您无需致电dataReader.Close();,因为它会在using block结束时自动处理。