我有一个GridView,我想要从表格中检查Task_ID
,如果存在,则更新记录,但如果表中不存在Task_ID
,则想要将其插入到我的表格中。 我现在的代码完成了插入部分,但没有完成更新代码的部分。我想知道如何在同一段代码中完成这个。请帮忙,谢谢。
以下是我的代码:
int index = 0;
foreach (GridViewRow row in myGV.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label ID = row.FindControl("lbl_ID") as Label;
string UID = Request.Form[row.FindControl("hfUId").UniqueID];
DateTime strDate = DateTime.Now;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myCon"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into myTable(TID, USR_ID, UPDT_DT) values(@ID, @USR_ID, @UPDT_DT) ";
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID.Text;
cmd.Parameters.Add("@USR_ID", SqlDbType.VarChar).Value = UID.ToString();
cmd.Parameters.Add("@UPDT_DT", SqlDbType.VarChar).Value = strDate.Date;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
cmd.Dispose();
}
index++;
}