【问题标题】:Update multiple checkboxlist values to SQL DB asp.net C#将多个复选框列表值更新为 SQL DB asp.net C#
【发布时间】:2015-10-07 19:22:34
【问题描述】:

我正在使用下面的代码更新 SQL DB 中的一行。循环工作并更新行,但问题是每次通过循环时,它只更新一个值,而其他值被覆盖。所以最后,它已经更新,但不是为各个项目 ID 输入多个值到表中,而是只为各个项目 ID 输入一个值。我没有收到任何错误。非常感谢任何帮助。

for (int i = 0; i < cbAvailableEntities.Items.Count - 1; i++)
{
    SqlConnection connection = new SqlConnection(connString);
    SqlCommand cmd = new SqlCommand("UpdateProjectEntity", connection);

    using (connection)
    {
        connection.Open();
        using (cmd)
        {
           if (cbAvailableEntities.Items[i].Selected)
            {
               cmd.CommandType = CommandType.StoredProcedure;
                //the following is the Project ID for the row being updated.
               SqlParameter paramPID = new SqlParameter("@ProjectID", nr.ProjectID);
               cmd.Parameters.Add(paramPID);
               nr.Entities = cbAvailableEntities.Items[i].Value;
               cmd.Parameters.AddWithValue("@CorpID", nr.Entities);
               cmd.ExecuteNonQuery();
           }
        }
    }
}

这里是存储过程"UpdateProjectEntity"的SQL查询

ALTER PROCEDURE [dbo].[UpdateProjectEntity]
    @ProjectID int,
    @CorpID int
AS
BEGIN
    UPDATE [dbo].[ProjectEntity]
    SET
           [CorpID] = @CorpID
     WHERE
           ProjectID = @ProjectID
END

这是我运行程序时输入和结果的屏幕截图。 These are the checkboxes I am saving to the DB

This is the result after I have saved to the DB

我更改了日期以表明该程序中的所有其他内容都正常。

【问题讨论】:

  • 我不明白这个问题。你能展示一些正在发生的事情的数据吗?
  • btw 为什么要在循环内打开连接?如果您打开一次连接并执行所有命令然后关闭它会更好吗?
  • 问题是每次执行查询时都会更新行。所以每次更新行时,它只用一个值更新它,然后当它完成循环时,只有最后一个值在 SQL 表中。因此,不是为一行更新多个值,而是只更新了一个值。
  • 什么是"UpdateProjectEntity"?如果那是一个 storeproc,你应该包含代码。
  • @JuanCarlosOropeza,我添加了存储过程的查询。

标签: c# mysql sql asp.net checkboxlist


【解决方案1】:

我可以看到你保存了你的实体INT,也许你应该将它保存为逗号分隔的字符串。

所以不用保存1,你可以保存1,2,3

当然,您必须在保存构建之前添加一些逻辑并连接字符串。并且当你从 db 读取时还需要做一些解析,通过,进行拆分

另一种方法是创建一个关系表来指示选择的选项。

但是,当您删除选择并添加新选择时,这也会出现问题。

ProjectID   CorpID
   1          1
   1          2
   1          3

【讨论】:

  • 如果它作为字符串传递,因为存储过程和表需要INT,它会出错。我过去曾尝试过使用字符串,但忘记了它必须是INT。我正在使用的数据库中有很多表,我的经理不希望其中有更多表。我可以做到,但这是额外的工作。
  • 正如我所说,除非你改变你的结构,否则你将无法解决它。我给你两个选择来做到这一点。我再给你一个ONETHIS
  • 我记得我上个月给的answer。如果您的复选框很少,您可以使用按位运算符将所有选项保存为整数。
【解决方案2】:

我在不对数据库进行任何更改的情况下解决此问题的方法是使用 DELETE 语句删除具有 ProjectID 的行,然后使用之前使用过的插入存储过程。这比在所有已经存在的表中创建另一个表要快得多。所以代码是这样的

for (int i = 0; i < cbAvailableEntities.Items.Count - 1; i++) {
SqlConnection connection = new SqlConnection(connString);
SqlCommand com = new SqlCommand("InsertProjectEntity", connection);
SqlCommand dcm = new SqlCommand();

using(connection) {
    //First time going through the loop, i = 0 is true.
    if (i == 0) {
        connection.Open();
        using(com) {
            //This will remove anything in the DB related to the ProjectID being edited.
            dcm.Connection = connection;
            dcm.CommandText = "DELETE FROM [dbo].[ProjectEntity] WHERE ProjectID = " + _pID;
            dcm.ExecuteNonQuery();
            //This will insert all items checked in the checkboxlist but will not insert the unchecked.
            if (cbAvailableEntities.Items[i].Selected) {
                com.CommandType = CommandType.StoredProcedure;
                SqlParameter paramPID = new SqlParameter("@ProjectID", nr.ProjectID);
                com.Parameters.Add(paramPID);
                nr.Entities = cbAvailableEntities.Items[i].Value;
                com.Parameters.AddWithValue("@CorpID", nr.Entities);
                com.ExecuteNonQuery();
            }

        }
    } else {
        connection.Open();
        using(com) {
            //This will insert all items checked in the checkboxlist but will not insert the unchecked.
            if (cbAvailableEntities.Items[i].Selected) {
                com.CommandType = CommandType.StoredProcedure;
                SqlParameter paramPID = new SqlParameter("@ProjectID", nr.ProjectID);
                com.Parameters.Add(paramPID);
                nr.Entities = cbAvailableEntities.Items[i].Value;
                com.Parameters.AddWithValue("@CorpID", nr.Entities);
                com.ExecuteNonQuery();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多