将多个复选框列表值更新为 SQL DB asp.net C#

Update multiple checkboxlist values to SQL DB asp.net C#

我正在使用下面的代码更新 SQL 数据库中的一行。循环有效并更新行,但问题是每次循环时,它只更新一个值而其他值被覆盖。所以最后,它已经更新,但不是将多个值输入到相应项目 ID 的 table 中,而是只为相应项目 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

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

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

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

而不是保存 1

当然,您必须在保存构建和连接字符串之前添加一些逻辑。当你从数据库中读取时,还需要做一些解析,按 ,

进行拆分

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

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

ProjectID   CorpID
   1          1
   1          2
   1          3

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

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();
            }
        }
    }
}