mssql查询以插入和更新文本框值

时间:2018-01-24 08:57:43

标签: c# sql-server

asp page我在asp页面中有两个文本框,按钮点击后会保存在DB中。我需要插入查询,如果值存在,则必须更新。你能否找到我的查询有什么问题?为什么它不起作用?

protected void save_returnedmails_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(devconnstring);
        con.Open();
        try
        {
            foreach (GridViewRow g1 in ReturnMailGrid.Rows)
            {
                string reasontxt = ((System.Web.UI.WebControls.TextBox)g1.FindControl("reasontxt")).Text;
                string actiontxt = ((System.Web.UI.WebControls.TextBox)g1.FindControl("actiontxt")).Text;
                string returnmailqry = "if exists(select * from EmailQ_Summary where dscLinkkey='" + g1.Cells[2].Text + "') begin update EmailQ_Summary set Reason='" + reasontxt + "',Action='" + actiontxt + "' where dscLinkkey='" + g1.Cells[2].Text + "' end else begin insert into EmailQ_Summary(dscLinkkey,Reason,Action) values('" + g1.Cells[2].Text + "','" + reasontxt + "','" + actiontxt + "') end";
                SqlCommand command = new SqlCommand(returnmailqry, con);
                command.ExecuteNonQuery();
            }
            Label1.Text = "Inserted Successfully";
        }
        catch (Exception ex)
        {
            Label1.Text += ex.Message.ToString();
        }
        finally { con.Close(); }
    }

0 个答案:

没有答案