【发布时间】:2014-05-25 10:27:44
【问题描述】:
以下是删除过期会话的 ASPState 存储过程。如果您有自己的服务器,则可以将其配置为自动运行。但是,在共享主机上,您必须按时间间隔从代码中调用它
ALTER PROCEDURE [dbo].[DeleteExpiredSessions]
AS
SET NOCOUNT ON
SET DEADLOCK_PRIORITY LOW
DECLARE @now datetime
SET @now = GETUTCDATE()
CREATE TABLE #tblExpiredSessions
(
SessionID nvarchar(88) NOT NULL PRIMARY KEY
)
INSERT #tblExpiredSessions (SessionID)
SELECT SessionID
FROM [luckysessions].dbo.ASPStateTempSessions WITH (READUNCOMMITTED)
WHERE Expires < @now
IF @@ROWCOUNT <> 0
BEGIN
DECLARE ExpiredSessionCursor CURSOR LOCAL FORWARD_ONLY READ_ONLY
FOR SELECT SessionID FROM #tblExpiredSessions
DECLARE @SessionID nvarchar(88)
OPEN ExpiredSessionCursor
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
WHILE @@FETCH_STATUS = 0
BEGIN
DELETE FROM [luckysessions].dbo.ASPStateTempSessions WHERE SessionID = @SessionID AND Expires < @now
FETCH NEXT FROM ExpiredSessionCursor INTO @SessionID
END
CLOSE ExpiredSessionCursor
DEALLOCATE ExpiredSessionCursor
END
DROP TABLE #tblExpiredSessions
RETURN 0
我通过我的代码调用它如下:
SqlConnection con = new SqlConnection("Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxx;");
SqlCommand comm = new SqlCommand("DeleteExpiredSessions", con);
comm.CommandType = CommandType.StoredProcedure;
con.Open();
int deleted = comm.ExecuteNonQuery();
con.Close();
问题是我想知道 StoredProcedure 删除了多少行,而 ExecuteNonQuery 将始终返回 -1
我不想编辑存储过程,但如果这是最终解决方案,那就这样吧。
【问题讨论】:
-
存储过程不返回删除的行数并且您“不想编辑存储过程”。所以你不能
-
但是,如果这是最终的解决方案,那就这样吧.. 但是如何?
-
如果无法编辑存储过程,我认为解决方案可能是计算执行命令前后的会话数
-
你需要
SET NOCOUNT OFF然后
标签: c# sql stored-procedures sql-server-2012