简体   繁体   中英

Stored Procedure works well in SSMS but no rows returning when it is called in C#

I have a stored procedure that I need to run in C# and set the result set returning from the SP in a HTML table. Please note that the SP is working well in SSMS and returning results.

The c# code I am using is (it is in an ASP 4.5 project):

SQLDatabase sqldb = new SQLDatabase();
using (SqlConnection sqlcn = new SqlConnection(sqldb.GetConnectionString().ToString()))
{
   if (sqlcn.State == ConnectionState.Closed)
   {
      sqlcn.Open();
   }
   SqlCommand cmd = new SqlCommand("[MyStoredProcedure]", sqlcn);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.AddWithValue("@FromDate", Convert.ToDateTime(txtFrom.Value.ToString()));
   cmd.Parameters.AddWithValue("@ToDate", Convert.ToDateTime(txtTo.Value.ToString()));
   using (SqlDataAdapter a = new SqlDataAdapter(cmd))
   {
       DataSet ds = new DataSet();
       a.Fill(ds);
       dtExtra = ds.Tables[0];
   }
}

This code above is returning 0 rows, even though the SP is working in SSMS. While debugging, the connectionstring and the parameters are coming all true, no issue. The connectionstring is:

<add name="DefaultDB" connectionString="Data Source=TestEnv;Initial Catalog=TestTable;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

I don't understand what may cause this. I found the topic below, but I am using Integrated Security=SSPI in my connection string already, so it did not help me. Any advice would be appreciated.

ASP.NET stored proc call bringing back no rows, but does in Management Studio!

EDIT: SOLVED! Thanks @NineBerry. It turned into a between/and usage problem in SP. Changing txtTo.Value as: DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"); in the code fixed the issue (It was DateTime.Today.ToString("yyyy-MM-dd") before, I should have included it in the code part, didn't think it is related to that, sorry). Better solution would be updating the SP using >= and <= instead of between/and keywords tho.

I would modify your code to simply be:

using(var dbConnection = new SqlConnection("..."))
using(var command = new SqlCommand(query, dbConnection))
{
     dbConnection.Open();
     ...
}

Handling the connection pooling in the using block is always a good idea per Microsoft guideline:

To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.

You are checking if the connection is closed, what if the connection is idle? By using the using syntax you implement dispose. So it will correctly close your connection, so you should not need to check if the connection is closed unless you are using a singleton for the connection.

After reading your question, you may have more than just the one issue I pointed out. I would recommend a service account with access the specific data you are seeking, that the application can access rather than integrated security.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM