我有一个按钮事件,在达到一定数量的用户后必须自行禁用。
我们正在将当前访问的用户计数存储在数据库中,并且我正在尝试创建一个存储过程,当调用该存储过程时,它将自动增加该表上的用户计数。
我将创建一个单独的过程来减去。
在我看来,我需要获取当前值,添加一个,然后将其保存在表中,将我带到以下代码:
CREATE PROCEDURE procDrRefBooksAddUser
-- Add the parameters for the stored procedure here
@fileId int = NULL,
@count int = 0,
@newCount int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT UserCount FROM tblDrRefBooks WHERE DrRefBookFileID = @fileId
SET @count = UserCount
SET @newCount = @count + 1
-- Insert statements for procedure here
UPDATE tblDrRefBooks SET tblDrRefBooks.UserCount = @newCount WHERE DrRefBookFileID = @fileId
END
不幸的是,我无法正确处理,出现以下错误:
Msg 207, Level 16, State 1, Procedure procDrRefBooksAddUser, Line 17
Invalid column name 'UserCount'
有人可以告诉我哪里出错了。
编辑:答案更新(最后)
我的 SQL 是:
ALTER PROCEDURE [dbo].[procDrRefBooksAddUser]
-- Add the parameters for the stored procedure here
@fileId int = NULL,
@newCount int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- SET NOCOUNT ON;
UPDATE tblDrRefBooks
SET @newCount = UserCount = UserCount + 1
WHERE DrRefBookFileID = @fileId
SELECT @newCount AS iden
END
后面的代码是:
using (SqlConnection db = DataConn.SqlConnection())
{
db.Open();
SqlTransaction transaction = db.BeginTransaction();
try
{
lbltest.Text = "Starting...";
using (SqlCommand acommand =
new SqlCommand(
"EXEC procDrRefBooksAddUser @fileID, @count",
db, transaction) { CommandType = CommandType.Text })
{
acommand.Parameters.Add(new SqlParameter("@fileID", SqlDbType.Int)).Value = Int32.Parse(location.Value);
acommand.Parameters.Add(new SqlParameter("@count", SqlDbType.Int) { Direction = ParameterDirection.Output });
using (SqlDataReader rdr = acommand.ExecuteReader())
{
btnLoad.Text = "Here: " + rdr["iden"];
}
}
transaction.Commit();
}
catch (Exception ea)
{
transaction.Rollback();
lbltest.Text = ("Insertion Error: " + ea.Message);
}
}
但我得到这个错误:Insertion Error: Invalid attempt to read when no data is present.