我有一个存储过程,从SSMS调用并运行成功后需要大约10秒才能运行。该过程将int
作为参数。
从代码中调用相同的存储过程时:
using (var connection = new SqlConnection(ConnectionStringName))
{
using (var cmd = new SqlCommand("ProcedureName", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@itemId", itemId));
cmd.CommandTimeout = 150;
connection.Open();
cmd.ExecuteNonQuery();
}
}
我得到的错误如下:
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.
The timeout period elapsed prior to completion of the operation or the server is not responding. --->
System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
传递的参数有效,当从SSMS调用存储过程时,它使用相同的参数值正确执行。
答案 0 :(得分:2)
您可能忘记指定参数的方向,因为您可以提供输入和输出参数。试试这是否有效:
SqlParameter param = new SqlParameter("@itemId", itemId);
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);
答案 1 :(得分:2)
要避免该错误,请使用:
CREATE TABLE orders_master (
order_id NUMBER(1) NOT NULL
,order_total NUMBER(10) NULL
)
/
CREATE TABLE monthly_orders (
order_id NUMBER(1) NOT NULL
,order_total NUMBER(10) NULL
)
/
INSERT INTO orders_master (order_id, order_total) VALUES (1, 1000)
/
INSERT INTO orders_master (order_id, order_total) VALUES (2, 2000)
/
INSERT INTO orders_master (order_id, order_total) VALUES (3, 3000)
/
INSERT INTO orders_master (order_id, order_total) VALUES (4, NULL)
/
INSERT INTO monthly_orders (order_id, order_total) VALUES (2, 2500)
/
INSERT INTO monthly_orders (order_id, order_total) VALUES (3, NULL)
/
MERGE INTO orders_master o
USING monthly_orders m
ON (o.order_id = m.order_id)
WHEN MATCHED THEN
UPDATE SET o.order_total = m.order_total
DELETE WHERE m.order_total IS NULL
WHEN NOT MATCHED THEN
INSERT VALUES (m.order_id, m.order_total)
/
COMMIT
/
SQL> select * from orders_master
2 /
ORDER_ID ORDER_TOTAL
---------- -----------
1 1000
2 2500
4
注意:
您的查询执行将花费不定时间。