Search code examples
c#asp.netsql-server-ceiis-8

C#/ASP.NET Web Site using SQL Server CE Database Slow Query


The main issue I have is that a simple select from my SQL Server CE database has a typical wait time of 17 seconds for the first query and ~5 seconds for each subsequent query.

After letting the site sit idle for a couple minutes, the 17 second delay comes back. I am assuming the delay is from establishing connections, caching, etc, but I feel on our LAN this is a ridiculous wait and will only get worse for outside users.

The site is a c#/ASP.NET web site that is using a SQL Server Compact database (v4.0.8876.1). This is deployed to an IIS8 server. The site is intended to calculate values from user input using coefficients for different models that are stored in the database.

What am I doing wrong for there to be such a massive delay during the connection/queries?

Connection string:

<add name="DatabaseConnection" 
     connectionString="Data Source=|DataDirectory|\Database.sdf" 
     providerName="System.Data.SqlServerCe.4.0"/>

Query:

string connstr = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;

using (SqlCeConnection conn = new SqlCeConnection(connstr))
{
    string queryString = @"Select id, description, imagePath
                           from ConservationModels
                           where model = @modelName";

    SqlCeCommand command = new SqlCeCommand(queryString, conn);
    command.Parameters.AddWithValue("@modelName", modelName);

    try
    {
        conn.Open();

        SqlCeDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            modelID = (int)reader[0];
            description = (string)reader[1];
            imagePath = (string)reader[2];
        }

        reader.Close();
     }
     catch (Exception ex)
     {
        throw;
     }
 }

Apologies for any missing information and I appreciate any suggestions. If there is a better way to achieve the goal for a web site, I am all ears.


Solution

  • Following MatteoSp's advice, I removed SQL Server CE components from the site and created a SQL Server Express database to replace it. Though the compilation time on the site seems to have increased for some reason, this resolved my issue for the slow queries/connection.

    Thank you for the assistance.