Вопрос

I have the following code and I was wondering if anyone knew the correct way to handle this.

SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["someConnectionString"].ConnectionString);
SqlCommand objComm = new SqlCommand("usp_someStoredProcedure", objConn);
objComm.CommandType = CommandType.StoredProcedure;
objComm.Parameters.AddWithValue("@Variable1", VOne);
objComm.Parameters.AddWithValue("@Variable2", VTwo);

objConn.Open();
using (IDataReader dr = objComm.ExecuteReader(CommandBehavior.CloseConnection))
{
   //do stuff
}

Now, let's say the stored procedure returns nothing, is there a method to handle this?

Это было полезно?

Решение

Normally the section you have marked with //do stuff would contain a

if (dr.Read())
{
  // do stuff
}

or a

while (dr.Read())
{
  // do stuff
}

The .Read() check ensures that you're only acting if it returned data.

Другие советы

while (dr.Read())
    {
        Console.WriteLine(String.Format("{0}", reader[0]));
    }

you can refer from http://msdn.microsoft.com/en-us/library/y6wy5a0f.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top