C#将数据库设置为我想要的特定数据

时间:2019-01-31 08:32:08

标签: c# sql-server

我目前正在尝试删除我的广告。但是,除了将其从数据库中删除之外,我只想将状态从1(表示活动)设置为0(表示无效)。我尝试使用查询UPDATE。但是我不知道格式。我当前的代码是

protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (sqlCon.State == ConnectionState.Closed)
            sqlCon.Open();
        SqlCommand sqlCmd = new SqlCommand("DeleteImage", sqlCon);
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.AddWithValue("AdvID", Convert.ToInt32(hfContactID.Value));
        sqlCmd.ExecuteNonQuery();
        sqlCon.Close();
        Clear();
        FillGridView();
        LitMsg.Text = "Deleted Successfully";
        ButSave.Enabled = true;
        Image1.Visible = false;
    }

并且我相信Delete查询不会将我的状态更改为0,所以我的更新查询就是这样。

protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (FileImgsave.HasFile == true)
        {


            string imgfile = Path.GetFileName(FileImgsave.PostedFile.FileName);
            //FileImgsave.SaveAs("Images/" + imgfile);
            FileImgsave.SaveAs(Server.MapPath("~/Images/" + imgfile));
            sqlCon.Open();
            SqlCommand cmd = sqlCon.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "UPDATE Advertisement SET Item=@item,ImgPath=@image,Name=@name Where AdvID='" + AdsTb.Text + "'";
            cmd.Parameters.AddWithValue("@name", nameTb.Text);
            cmd.Parameters.AddWithValue("@item", imgfile);
            cmd.Parameters.AddWithValue("@image", "~/Images/" + imgfile);
            cmd.ExecuteNonQuery();
            sqlCon.Close();
            FillGridView();
            LitMsg.Text = "Update successfully!";
            Clear();
        }

下面是我的删除查询

ALTER PROC [dbo].[DeleteImage]
@AdvID int

AS
    BEGIN
    DELETE FROM Advertisement
    WHERE AdvID = @AdvID
    END

1 个答案:

答案 0 :(得分:1)

您忘记更新状态

cmd.CommandText = "UPDATE Advertisement SET Status=0, Item=@item,ImgPath=@image,Name=@name Where AdvID='" + AdsTb.Text + "'";

状态= 0