Sql超时错误,但为什么

本文关键字:为什么 错误 超时 Sql | 更新日期: 2023-09-27 17:51:24

下面的代码产生一个System.Data.SqlClient.SqlException: Timeout expired

const string sqlStmt = @"SELECT * 
                         FROM CUSTOMER_INFO 
                         WHERE CUSTOMER_NO = @CUSTOMER_NO;";
SqlCommand command = new SqlCommand(sqlStmt, connection);
command.Parameters.AddWithValue("@CUSTOMER_NO", txtAccountNo.Text.Trim().ToUpper());

但这不会超时…

const string sqlStmt = @"SELECT * 
                         FROM CUSTOMER_INFO 
                         WHERE CUSTOMER_NO = @CUSTOMER_NO;";
SqlCommand command = new SqlCommand(sqlStmt, connection);            
command.Parameters.Add("@CUSTOMER_NO", SqlDbType.VarChar, 25).Value = txtAccountNo.Text.Trim().ToUpper();
我不明白为什么,有谁能开导我一下吗?

Sql超时错误,但为什么

您可以查看一下数据库执行的SQL语句吗?

您可能会看到参数使用的类型有所不同。我相信AddParamWithValue方法不会为参数使用正确的类型。

然后,DBMS可能不得不将值转换回正确的类型。在这种情况下,一些DBMS将无法使用索引查找,这将导致更长的查询运行时间,因此出现超时。