查询不检查数据库 AdvID 本身并删除
Query is not checking database AdvID itself and delete
我希望查询检查数据库 AdvID 本身并在加载页面时删除。为什么我收到此错误调用 "System.Data.SqlClient.SqlException: 'Procedure or function 'DeleteByDate' expects parameter '@AdvID', which was not supplied." 它不应该检查数据库本身并删除吗?为什么我必须为其提供 AdvID?这是我的 page_load 代码
protected void Page_Load(object sender, EventArgs e)
{
//Generate auto ID
SqlDataAdapter sad = new SqlDataAdapter("Select isnull(max(cast(AdvID as int)),0)+1 from Advertisement", sqlCon);
DataTable dt = new DataTable();
sad.Fill(dt);
advertismentIdTb.Text = dt.Rows[0][0].ToString();
//PageLoadValidations
statusTb.Text = "1";
endDateTb.Attributes["min"] = DateTime.Now.ToString("yyyy-MM-dd");
btnDelete.Enabled = false;
btnUpdate.Enabled = false;
Image1.Visible = false;
//Delete from DB Condition (EndDate)
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("DeleteByDate", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
//Show GridView
FillGridView();
}
这是我的脚本
ALTER PROC [dbo].[DeleteByDate]
@AdvID int
AS
BEGIN
DECLARE @CurrentDate DATE = GETDATE() -- to get current date
-- Assuming that Advertisement table has the column EndDate
IF EXISTS (SELECT * FROM Advertisement a WHERE a.EndDate < @CurrentDate )
BEGIN
UPDATE a SET a.Status = 0
FROM Advertisement a
WHERE a.AdvID = @AdvID
END
END
我想你要求的查询将删除所有早于现在结束的广告?您当前的查询是检查是否有任何广告在此之前结束,然后删除了您传入的 ID 的广告。
基于以上假设,尝试如下查询:
ALTER PROC [dbo].[DeleteByDate]
AS BEGIN
UPDATE a
SET a.Status = 0
FROM Advertisement a
WHERE a.EndDate < GETDATE()
END
我希望查询检查数据库 AdvID 本身并在加载页面时删除。为什么我收到此错误调用 "System.Data.SqlClient.SqlException: 'Procedure or function 'DeleteByDate' expects parameter '@AdvID', which was not supplied." 它不应该检查数据库本身并删除吗?为什么我必须为其提供 AdvID?这是我的 page_load 代码
protected void Page_Load(object sender, EventArgs e)
{
//Generate auto ID
SqlDataAdapter sad = new SqlDataAdapter("Select isnull(max(cast(AdvID as int)),0)+1 from Advertisement", sqlCon);
DataTable dt = new DataTable();
sad.Fill(dt);
advertismentIdTb.Text = dt.Rows[0][0].ToString();
//PageLoadValidations
statusTb.Text = "1";
endDateTb.Attributes["min"] = DateTime.Now.ToString("yyyy-MM-dd");
btnDelete.Enabled = false;
btnUpdate.Enabled = false;
Image1.Visible = false;
//Delete from DB Condition (EndDate)
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("DeleteByDate", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
//Show GridView
FillGridView();
}
这是我的脚本
ALTER PROC [dbo].[DeleteByDate]
@AdvID int
AS
BEGIN
DECLARE @CurrentDate DATE = GETDATE() -- to get current date
-- Assuming that Advertisement table has the column EndDate
IF EXISTS (SELECT * FROM Advertisement a WHERE a.EndDate < @CurrentDate )
BEGIN
UPDATE a SET a.Status = 0
FROM Advertisement a
WHERE a.AdvID = @AdvID
END
END
我想你要求的查询将删除所有早于现在结束的广告?您当前的查询是检查是否有任何广告在此之前结束,然后删除了您传入的 ID 的广告。
基于以上假设,尝试如下查询:
ALTER PROC [dbo].[DeleteByDate]
AS BEGIN
UPDATE a
SET a.Status = 0
FROM Advertisement a
WHERE a.EndDate < GETDATE()
END