ASP.NET第一次访问数据库错误

时间:2013-05-28 16:18:18

标签: asp.net iis-7 sql-server-2008-r2

在我Application_Start()的应用程序中,我从DB加载了一些静态数据以供进一步使用。但问题是,当服务器空闲时,例如一天,然后对Db的第一个请求不成功并返回此错误:

The client was unable to establish a connection because of an error during connection initialization process before login.
Possible causes include the following:
the client tried to connect to an unsupported version of SQL Server;
the server was too busy to accept new connections;
or there was a resource limitation (insufficient memory or maximum allowed connections) on the server.

(provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

然后刷新后,一切正常。那么任何想法?为什么Application_Start()每天都会被调用?我认为只有在第一次请求应用程序时才会调用它,然后才会从IIS重新启动它。

编辑:
我正在使用SQL Server 2008R2快递
我认为我的代码应该没有问题,但她是:

    public static SqlConnection sqlCon()
    {
        return new SqlConnection(WebConfigurationManager.ConnectionStrings["Test"].ConnectionString);             
    }
public void fillFromDB()                                      
    {
        SqlConnection con = defVal.sqlCon();
        SqlCommand cmd_getK = new SqlCommand("usp_getAllKomponenty", con);
        cmd_getK.CommandType = CommandType.StoredProcedure;
        try
        {
            con.Open();
            SqlDataReader reader = cmd_getK.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    this.adKomp(reader[0].ToString(), new elisKomponent(reader[0].ToString(), reader[1].ToString(), new date(Convert.ToInt32(reader[2]))));
                }
                reader.Close();
            }
        }
        catch (SqlException e)
        {
            throw new ApplicationException("Chyba při čtení komponenty databáze"+e.Message);
        }
        finally
        {
            con.Close();
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试更多/最具体的连接字符串类型。

IP地址,端口号。网络协议。

Data Source=192.168.1.333,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;
User ID=myUsername;Password=myPassword;

这可能会有所帮助......因为它将摆脱一些“解决”支票。