简体   繁体   English

运行INSERT INTO SQL语句时超时已过期

[英]Timeout expired while running INSERT INTO SQL statement

I want to insert data using SQL INSERT INTO statement, but an error Timeout expired occur. 我想使用SQL INSERT INTO语句插入数据,但是发生超时超时错误。 Anybody can help me. 任何人都可以帮助我。 Thanxs. 谢谢。 The code are like this. 代码是这样的。

SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString);                
            SqlCommand sqlCom = new SqlCommand();
            sqlCom = sqlConn.CreateCommand();                

            var strField = new List<string>();
            var strVal = new List<string>();                

            if (this.Row.TriggerEntityID != null)
            {
                sqlCom.Parameters.AddWithValue("@EntityId", this.Row.TriggerEntityID);
                strField.Add("EntityID");
                strVal.Add("@EntityId");
            }

            if (this.Row.TriggerProjectID != null)
            {
                sqlCom.Parameters.AddWithValue("@ProjectId", this.Row.TriggerProjectID);
                strField.Add("ProjectID");
                strVal.Add("@ProjectId");
            }

            sqlCom.Parameters.AddWithValue("@EngagementId", this.Row.Id);

            string[] arrField = strField.ToArray();
            string[] arrVal = strVal.ToArray();

            string fld = arrField[0];
            string val = arrVal[0];
            for (var j = 1; j < arrField.Length; j++)
            {
                fld = fld + "," + arrField[j];
                val = val + "," + arrVal[j];
            }                

            sqlCom.CommandText = "INSERT INTO Engagement_Project_Entity (EngagementID, " + fld + ") VALUES (@EngagementId, " + val + ")";

            sqlConn.Open();
            sqlCom.ExecuteNonQuery();
            sqlConn.Close();    

Am I missing something? 我想念什么吗? or the sequence is wrong? 还是顺序错误?

If you got a timeout exception during a query execution, that clearly shows that your C# code managed to pass the query over to the database and the database tried executing the query, but it couldn't complete the execution before the timeout. 如果在查询执行期间遇到超时异常,则清楚地表明您的C#代码成功将查询传递给数据库,并且数据库尝试执行查询,但是在超时之前无法完成执行。

So here are few troubleshooting steps I would take. 因此,这里有一些我将要采取的故障排除步骤。

  1. First make sure the timeout you are getting is an execution timeout and not a connection timeout. 首先,确保您获得的超时是执行超时,而不是连接超时。 If it is connection timeout, that means the SQL Server is not accessible at the moment. 如果是连接超时,则表示目前无法访问SQL Server。 If it is a query execution timeout, follow the below steps to find out exactly what is causing the query to timeout. 如果这是查询执行超时,请按照以下步骤查找导致查询超时的确切原因。
  2. Use SQL Server Profiler (From Tools menu in SQL Server Management Studio) to find out the exact query that is getting fired when this piece of code executes. 使用SQL Server Profiler(从SQL Server Management Studio的“工具”菜单中)找出执行这段代码时将触发的确切查询。
  3. Check if too many fld and value pairs are generated by the code. 检查代码是否生成了太多的fld和值对。
  4. Try executing the same query from SQL Server Management Studio and find out if the timeout is reproducible. 尝试从SQL Server Management Studio执行相同的查询,并确定超时是否可重现。 If you get the same problem from SQL Server Management Studio as well, fix the query. 如果您从SQL Server Management Studio中也遇到相同的问题,请修复查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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