¿El lector Sql está leyendo una fila nula?

I have a table in SQL which literally got no rows but one that says null(which I guess is by default) now if I read it, it goes through reader at least once, here is what I am trying to do.

            int Count = 0;

            SqlCommand cmd1 = new SqlCommand("Exists", conn);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add(new SqlParameter("@Url", URL));
            SqlDataReader rdr1 = cmd1.ExecuteReader();


            if (rdr1.Read())
            {
                Count++;
            }

how can I check if Rdr row is null and dont do Counter

Editar

Stored Procedure (I am not allowed to make changes to it)

CREATE PROCEDURE [dbo].[Exists]
(
    @Url varchar(255)
)
AS
    SET NOCOUNT ON;
SELECT COUNT(*) AS Rows FROM Instances 
WHERE Url = @Url

preguntado el 31 de julio de 12 a las 11:07

A row that contains nulls is still a row. There is no concept of a null row, btw; just a row that may have null values. -

Por favor, hazlo: select count(1) from YourTable - that will tell you whether it has rows or not. Also, even if there are no rows, a sproc could still incorrectly return a row. Can you show the sproc? -

K... That SP will always return exactly one row. Zero still counts! -

4 Respuestas

If the reader is reporting true de Read(), then your stored procedure is definitely returning a row (or rows).

If you call the SP directly (SSMS etc), that will quickly tell you whether a row is returned. A row with a null in every column is still a row. Dos opciones:

  • fix the SP such that it doesn't return rows incorrectly (I'm guessing it is selecting into variables. And then selecting the variables: that will always yield a row)
  • change the reader code to ignore impossible data

For example. If the first column must be non-null for a "real" row, then simply:

if(!reader.IsDBNull(0)) Count++;

Which ignores the row if the first cell is null.

Respondido 31 Jul 12, 12:07

Instead using reader why dont you just use ExecuteScaler()? that is better option for retrieving a single value from database.

In case, there is specific requirement to use reader then you will need to check for

if( reader.Read() ==true && reader[0] !=DBNull.Value)
{
count++;
} 

Here is, how you may use ExecuteScalar: In case, nothing found in database then it will return 0.

             int Count = 0;
            using (SqlConnection conn = new SqlConnection ( " Your ConnectionString" ))
            {

                using (SqlCommand cmd1 = new SqlCommand ( "Exists", conn ))
                {

                    cmd1.CommandType = CommandType.StoredProcedure;
                    cmd1.Parameters.Add ( new SqlParameter ( "@Url", URL ) );

                    conn.Open ();

                    if( Convert.ToInt32 ( cmd1.ExecuteScalar () ) > 0 )
                    {
                        Count +=1;
                    }
                    conn.Dispose ();

                }
            }

if there is not specific requirement to use Count+=1; then following lines will serve batter

Count = Convert.ToInt32 ( cmd1.ExecuteScalar () );

Respondido 01 ago 12, 05:08

Puedes usar DBNull.Value or IsDBNull(column_ordinal) method to test the value of a specific column is null or not.

Respondido 31 Jul 12, 12:07

am not sure, how I will gonna use it tho :s - fdgfdgs dfg

Nombre - You are missing the Memory Management. The code should be like below...

int Count = 0;
using (SqlCommand cmd1 = new SqlCommand("Exists", conn))
{
    cmd1.CommandType = CommandType.StoredProcedure;
    cmd1.Parameters.Add(new SqlParameter("@Url", URL));
    using (SqlDataReader rdr1 = cmd1.ExecuteReader())
    {
        if (rdr1.Read())
        {
            Count++;
        }
    }
}

Count will always return something, either 0 record or more then 0. So, you will always receive a row which will have count of rows for a particular URL.

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.