SqlDataReader Can't Proceed into While

时间:2018-02-03 08:25:07

标签: c# asp.net sql-server

I have a problem in SqlDataReader - it cannot proceed into while and cannot while.

Here is my code

List<tmp_WatchList> data = new List<tmp_WatchList>();

using (SqlConnection con = new SqlConnection(conStr))
{
    using (SqlCommand cmd = new SqlCommand("sp_CheckPersonList", con)) 
    {
        try
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;

            SqlDataReader oReader = cmd.ExecuteReader();

            while (oReader.Read())
            {
                //data.Add(new tmp_WatchList
                //{
                tmp_WatchList l = new tmp_WatchList();
                l.id = int.Parse(oReader["id"].ToString());
                l.Name = oReader.GetValue(1).ToString();
                l.Crime = int.Parse(oReader.GetValue(2).ToString());
                data.Add(l);
                ///});
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }
}

and my stored procedure is:

ALTER PROCEDURE [dbo].[sp_CheckPersonList]
    (@Name NVARCHAR(MAX) NULL)
AS 
BEGIN
    SELECT REPLACE(Name, '.', ''), Crime
    FROM [dbo].[tmp_WatchList]
    WHERE [Name] LIKE CONCAT('%', REPLACE(@Name, ' ', '%'), '%')
END

Can you tell me how it is done? Or is something wrong with my structure?

3 个答案:

答案 0 :(得分:1)

You are not opening the connection any where before calling the ExecuteReader, you need to open the database connection, following is the lineo of code to open the connection :

con.Open(); // open connection
SqlDataReader oReader = cmd.ExecuteReader(); // now execute SP

and you do not need finally block for closing the connection, as you are already applyuing the using block on your SqlConnection and SqlCommand which is converted by compiler in to try finally which takes care of disposing the resources and in case of SqlConnection closing the connection.

答案 1 :(得分:0)

As other have pointed out, you need to Open the connection, and you can simplify your code removing the try/catch/finally and the explicit con.Close(), which you don't need since you are (corretcly) wrapping the connection within a using

Your code should be something like this (much cleaner than the original one after removing the try/catch/finally):

List<tmp_WatchList> data = new List<tmp_WatchList>();

using (SqlConnection con = new SqlConnection(conStr))
{
    con.Open();
    using (SqlCommand cmd = new SqlCommand("sp_CheckPersonList", con)) 
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;

        SqlDataReader oReader = cmd.ExecuteReader();

        while (oReader.Read())
        {
            tmp_WatchList l = new tmp_WatchList();
            l.id = int.Parse(oReader["id"].ToString());
            l.Name = oReader.GetValue(1).ToString();
            l.Crime = int.Parse(oReader.GetValue(2).ToString());
            data.Add(l);
        }
    }
}

If that code raises an exception, it will simply be forwarded to the caller, in a better way comparing to what you did with your throw new Exception(exc.Message), which will loose the original stack trace

答案 2 :(得分:0)

Remove the unwanted code..Try Like this..



 List<tmp_WatchList> data = new List<tmp_WatchList>();
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd=new SqlCommand();
    cmd.CommmandText="sp_CheckPersonList";
    cmd.CommandType = CommandType.Text;
    con.Open();
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@Name",name);
    SqlDataReader oReader = cmd.ExecuteReader();

            while (oReader.Read())
            {
                tmp_WatchList l = new tmp_WatchList();
                l.id = int.Parse(oReader["id"].ToString());
                l.Name = oReader.GetValue(1).ToString();
                l.Crime = int.Parse(oReader.GetValue(2).ToString());
                data.Add(l);
            }

    oReader.Close();
    Con.Close();