Asp.Net: returning a reader from a class

I was just wondering about the correct way to get the reader back from the class?

My code below works, but I'm not sure if this is correct.

Besides. I cannot close the connection in the class method and still access it from my ascx page,

what is OK?

// In my class, I have the following method for returning a record / read - this is the only record in this case.

public SqlDataReader GetPost()
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("con_spPost", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@blogid", blogid);
        try
        {
            conn.Open();
            return cmd.ExecuteReader();
        }
        finally
        {
          //  conn.Close();
        }
    }

// Then I call the GetPost method on my ascx page as follows:

protected void Page_Load(object sender, EventArgs e)
{

    //instantiate our class
    MyClass DB = new MyClass();

    //pass in the id of the post we want to view
    DB.PostID = Int32.Parse(Request.QueryString["p"]);

    ///call our GetPost method
    SqlDataReader reader = DB.GetPost();

   //output the result
    reader.Read();
    this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>";
    this.viewpost.InnerHtml += "<span class='blogheads'>" + reader["BlogTitle"].ToString() + "</span><p><p>";
    this.viewpost.InnerHtml += reader["BlogText"].ToString();
    reader.Close();
}

I would appreciate any comments on my code or advice, thanks.

melt

+3
source share
8 answers

, , , - , .

, IDbConnection GetPost, , , . using :

protected void Page_Load(object sender, EventArgs e) {

    // Create the DB, get the id, etc.    

    using (IDbConnection connection = new SqlConnection(connectionString))
    using (IDataReader reader = DB.GetPost(connection)) {
        reader.Read();
        this.viewpost.InnerHtml = reader["BlogText"].ToString();
        // finishing doing stuff with the reader  
    }
}

, - . , . DB.GetPost .

+6

, , ExecuteReader :

return cmd.ExecuteReader(CommandBehavior.CloseConnection);

te try/finally.

, Page_Load using, :

using (SqlDataReader reader = DB.GetPost()) {

    //output the result
    reader.Read();
    this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>"
        + "<span class='blogheads'>" + reader["BlogTitle"].ToString() + "</span><p><p>"
        +  reader["BlogText"].ToString();
}

, , SQL- - , :

if (!reader.Read()) {
    Something wrong
}

, , ,, HTML-, XSS, Server.HtmlEncode.

:

    this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>"
        + "<span class='blogheads'>" + Server.HtmlEncode(reader["BlogTitle"].ToString()) + "</span><p><p>"
        + Server.HtmlEncode(reader["BlogText"].ToString());
+5

. . , GetPost, , - DataReader. - ExecuteReader :

cmd.ExecuteReader(CommandBehavior.CloseConnection)

, , .

datareader , , . (A), datatable, . , GetPost , . ( B) GetPost, Using/Dispose . A.

+2

- ? ? , , .

0

. CSharpAtl, . , , .

, , try-finally. Close finally , , .

SqlDataReader reader;
try
{
///call our GetPost method
    reader = DB.GetPost();

   //output the result
    reader.Read();
    this.viewpost.InnerHtml = "<span id='post1_CreatedDate'>" + reader["CreatedDate"].ToString() + "</span><br>";
    this.viewpost.InnerHtml += "<span class='blogheads'>" + reader["BlogTitle"].ToString() + "</span><p><p>";
    this.viewpost.InnerHtml += reader["BlogText"].ToString();
}
finally
{
    reader.Close();
}
0
0

. n- . , , - . sql, , .

, , , .

, .

0

Source: https://habr.com/ru/post/1726893/


All Articles