简体   繁体   中英

Timeout expired while running INSERT INTO SQL statement

I want to insert data using SQL INSERT INTO statement, but an error Timeout expired occur. 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.

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. 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.
  3. Check if too many fld and value pairs are generated by the code.
  4. Try executing the same query from SQL Server Management Studio and find out if the timeout is reproducible. If you get the same problem from SQL Server Management Studio as well, fix the query.

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