【发布时间】:2019-07-01 02:07:21
【问题描述】:
我希望查询检查数据库 AdvID 本身并在页面加载时删除。为什么我收到此错误调用“System.Data.SqlClient.SqlException:'过程或函数'DeleteByDate'需要参数'@AdvID',但未提供。”它不应该检查数据库本身并删除吗?为什么我必须提供 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
【问题讨论】:
标签: c# sql sql-server tsql ado.net