-1
try
{
   SqlCommand cmd = new SqlCommand
   {
      Connection = con,
      CommandText = "GetJournalByTag",
      CommandType = System.Data.CommandType.StoredProcedure
   };

   cmd.Parameters.AddWithValue("@Tag", Tag);

   con.Open();
   cmd.ExecuteReader();
   con.Close();

   return 1;
}
catch
{
   return 0;
}

I have code written like this.

I plan to select posts with the same tag, sort them by date, and import them.

Here's what I'm curious about:

  1. If there are 100 posts, I would like to divide them into 10 pages, 10 each. In this case, what would be the best way to implement it? Any examples or links would be appreciated.
  2. If I return a value without con.close, does the SqlConnection close? What are the downsides if it doesn't close?
  3. I want to load multiple articles, but I plan to import the articleModel class from a SQL Server stored procedure. When executing the procedure, the Select * from article code is executed, in this case returns multiple rows. How can I read these multiple rows?
  4. Is there any difference in speed if I use a SQL query like select * from article? Is it better to specify all columns? Or is * better?
  5. Please let me know if you have any coding tips or tips to improve performance when configuring SQL Server or ASP.NET websites!

Thanks for reading.

  • Aside... [Can we stop using AddWithValue() already?](https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) [AddWithValue is Evil](https://www.dbdelta.com/addwithvalue-is-evil/). Use the other [.Add(...) methods](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlparametercollection#methods) so that you can specify the correct SQL data types, parameter lengths, etc.. – AlwaysLearning Dec 11 '21 at 10:28
  • 1
    Welcome to Stack Overflow. That's many questions into one, which is not following the guidelines here. Please read the [tour] and [ask], Consider researching individual ones, and ask them independently if you don't find an suitable answer. I suspect many are already covered by existing Q&A on Stack Overflow, though. – Pac0 Dec 11 '21 at 10:28
  • Second aside... the [SqlDataReader](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader?view=dotnet-plat-ext-6.0) returned from `ExecuteReader()` is an `IDisposable`. Be sure to close and dispose of it correctly so that you don't use up all of the connections in your connection pool. Also, be sure to handle `Read()` and `NextResult()` correctly otherwise you can miss errors and exceptions throw by SQL Server, ref: Dan Guzman's [The Curious Case of Undetected SQL Exceptions](https://www.dbdelta.com/the-curious-case-of-undetected-sql-exceptions/). – AlwaysLearning Dec 11 '21 at 10:32
  • Aside #3: `return 1;` doesn't make sense, you are not actually doing anything with the reader – Charlieface Dec 11 '21 at 21:36

1 Answers1

1

If there are 100 posts, I would like to divide them into 10 pages, 10 each

Consider using OFFSET-FETCH where in code you need to keep track of what has been fetched currently e.g. first time 0, next 10 etc. In the example below you pass in a parameter value for @Offset.

Using OFFSET and FETCH to limit the rows returned

See Also SO posts

And note for using * vs specifying columns, always specify columns.

DECLARE @Offset int = 0;
DECLARE @FetchRowCount int = 10;

SELECT P.ProductID, 
       P.ProductName, 
       P.CategoryID, 
       P.UnitPrice, 
       P.DiscontinuedDate, 
       C.CategoryName
FROM Products AS P
     INNER JOIN Categories AS C ON P.CategoryID = C.CategoryID
ORDER BY P.ProductName
OFFSET @Offset ROWS FETCH NEXT @FetchRowCount ROWS ONLY;

C# code model for connection and command

public static void Demo(string tag)
{
    using (var cn = new SqlConnection())
    {
        using (var cmd = new SqlCommand())
        {
            cmd.Connection = cn;
            cmd.CommandText = "GetJournalByTag";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Tag", SqlDbType.NVarChar).Value = tag;
        }
    }
}
Karen Payne
  • 4,341
  • 2
  • 14
  • 31