小编Rar*_*res的帖子

INSERT与更新

我有以下查询:

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();
}
Run Code Online (Sandbox Code Playgroud)

我需要的是看表中是否有任何记录,如果有更新,否则插入它.我怎么能实现呢?

c# sql asp.net

1
推荐指数
1
解决办法
961
查看次数

标签 统计

asp.net ×1

c# ×1

sql ×1