插入语句where子句C# [英] Insert statement with a where clause c#

查看:122
本文介绍了插入语句where子句C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入到细节依赖于从cookie值的表。这里是code

I am trying to insert details into a table depending on value from a cookie, . here is the code

   SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        conn.Open();
        string insertQuery = "insert into Details (Employees, Performance, TotalPerformance, Attitude, TotalAttitude) values(@employees, @performance ,@totalPerformance, attitude, totalAttitude)";
        SqlCommand com = new SqlCommand(insertQuery, conn);
        com.Parameters.AddWithValue("@employees", Request.QueryString["employees"]);
        com.Parameters.AddWithValue("@performance", totalPer.ToString());
        com.Parameters.AddWithValue("@totalPerformance", totalPercent.ToString());
        com.Parameters.AddWithValue("@attitude", totalAtt.ToString());
        com.Parameters.AddWithValue("@totalattitude", totalPercent.ToString());



        com.ExecuteNonQuery();

这工作正常,但在串插入查询行我想WHERE子句,其中,如果列名之称的行中的值的cookie匹配,将值插入到某一行中添加。我不知道正确的语法
感谢您的帮助。

This works fine but in the string insert query line I want to add in a where clause, where it will insert the values into a certain row if the value for column "name" in said row matches a cookie . I don't know the proper syntax Thanks for any help

推荐答案

您需要的 更新 声明,而不是一个插入语句,因为你要修改现有的记录。

You need an UPDATE statement, not an INSERT statement, since you are going to modify the existing record.

using (
    SqlConnection conn =
        new SqlConnection(
            ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString))
{
    conn.Open();
    string updateQuery =
        @"UPDATE Details 
        SET Employees = @employeee, 
        Performance = @performance , 
        TotalPerformance = @totalPerformance,
        Attitude = @attitude,
        TotalAttitude = @totalattitude
        WHERE yourField = @yourConditionValue";

    using (SqlCommand com = new SqlCommand(updateQuery, conn))
    {
        com.Parameters.AddWithValue("@employees", Request.QueryString["employees"]);
        com.Parameters.AddWithValue("@performance", totalPer.ToString());
        com.Parameters.AddWithValue("@totalPerformance", totalPercent.ToString());
        com.Parameters.AddWithValue("@attitude", totalAtt.ToString());
        com.Parameters.AddWithValue("@totalattitude", totalPercent.ToString());
        com.Parameters.AddWithValue("@yourConditionValue", yourValue);
        com.ExecuteNonQuery();
    }
}

+1,对于你的问题,另一件事情,使用参数附上命令连接对象 使用语句。

这篇关于插入语句where子句C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆