我有以下查詢:INSERT對UPDATE
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["chestionar"].ConnectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,'1',@ip,@idsesiune)", con);
cmd.Parameters.AddWithValue("@cnp", Session["sesiune_cnp"]);
cmd.Parameters.AddWithValue("@raspuns", textbox1.Text);
cmd.Parameters.AddWithValue("@data", DateTime.Now.ToLocalTime());
cmd.Parameters.AddWithValue("@ip",ip);
cmd.Parameters.AddWithValue("@idsesiune", id_sesiune);
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect("User2.aspx");
}
catch (Exception ex)
{
Console.WriteLine("Error:" + ex);
}
finally
{
con.Close();
}
我需要的是看是否有表中的任何記錄,如果沒有比別人更新插入it.How我能做到這一點?
什麼定義了一個紀錄表中已經存在?什麼是獨特的數據?本質上,你只需要首先做一個SELECT(或COUNT)查詢來查找數據,如果它在那裏,你可以做一個UPDATE而不是一個INSERT – musefan 2012-03-07 10:31:04