【问题标题】:.net api connection error.net api 连接错误
【发布时间】:2020-08-29 05:11:29
【问题描述】:

超时。在从池中获取连接之前超时时间已过。这可能是因为所有池连接都在使用中并且达到最大池大小

我的连接字符串在最长时间内出现此错误

我有这个错误的最大时间

我的 api 是这样的

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        //string valuess = Commanfunction.logintokencheck(user_id, token);
        try
        {                
            //if (valuess == "True")
            //{
                DataTable dt = new DataTable();
                using (SqlCommand cmd = new SqlCommand("SELECT * FROM [nwt].[usermessage] where [to_user] = '" + user_id + "' AND [msg_user_type] ='inbox' AND [reviwe] =1 AND ([status]='active' OR [status]='keep')", sconn))
                {
                    sconn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);

                    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                    Dictionary<string, object> row;
                    string x = "{\"status\":\"200ok\",\"reviewcount\":" + dt.Rows.Count;
                    // string y= serializer.Serialize(x);
                    //foreach (DataRow dr in dt.Rows)
                    //{
                    //    row = new Dictionary<string, object>();
                    //    foreach (DataColumn col in dt.Columns)
                    //    {
                    //        row.Add(col.ColumnName, dr[col]);
                    //    }
                    //    rows.Add(row);
                    //}
                    //string z = serializer.Serialize(rows);
                    return new RawJsonActionResult(x + "}");
                    //return (x + "\"countries\":" + z + "}");

                }

我怎样才能解决这个错误并优化我的 api。给出解决方案

【问题讨论】:

  • 什么是sconn?这里缺少很多信息。
  • SqlConnection sconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);

标签: asp.net asp.net-web-api


【解决方案1】:

可能尝试在您的连接字符串中定义一个池大小,如下所示

<add name="ConnString" connectionString="Data Source=53.127.121.91;  
    Initial Catalog=nhybridtewft;User ID=sa;Password=admin;  
    Integrated Security=False;Max Pool Size=1024;Pooling=true;  
    multipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>

您可以尝试增加数字并查看错误是否消失。我猜你可以声明的最大池大小是 32767。

【讨论】:

    猜你喜欢
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2019-08-30
    相关资源
    最近更新 更多