首页 > 解决方案 > 通过复选框更新列和多个更新

问题描述

感觉这应该不是一件容易的事。如何通过带有列名的复选框更新我的 sql server 表;

DROP TABLE #indebtedness
CREATE TABLE [dbo].[indebtedness](
[Subscriber_No] [bigint] NULL,
[Subscriber_Name] [nvarchar](max) NULL,
[Last_Deact_Date] [date] NULL,
[allocated] [decimal](18, 3) NULL,
[collected] [numeric](18, 3) NOT NULL,
[ID_No] [bigint] NULL,

我正在使用带有 c# 代码的桌面应用程序,我需要检查框以从 dataGridView 更新列

我使用类似这样的代码来更新我的数据库

int countSuccess = 0;

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    // INSERT command:
    using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET autodialtype=@autodialtype,autodial=@autodial,autodate=@autodate,lastdate=@lastdate,lastcall_case=@lastcall_case WHERE  Subscriber_No=@Subscriber_No and company_name=@company_name and indebtedness_name=@indebtedness_name", con))
    {
        com.Parameters.AddWithValue("@company_name", company_name.Text);
        com.Parameters.AddWithValue("@indebtedness_name", indebtedness_name.Text);
        com.Parameters.AddWithValue("@Subscriber_No", dataGridView1.Rows[i].Cells[0].Value);
        com.Parameters.AddWithValue("@autodialtype", dataGridView1.Rows[i].Cells[1].Value);
        com.Parameters.AddWithValue("@autodial", "Called");
        com.Parameters.AddWithValue("@autodate", DateTime.Today.ToShortDateString());
        com.Parameters.AddWithValue("@lastcall_case", "AutoDial");
        com.Parameters.AddWithValue("@lastdate", DateTime.Today.ToShortDateString());

        int numUpd = com.ExecuteNonQuery();

        countSuccess += numUpd;

        //com.ExecuteNonQuery();
    }
}

MessageBox.Show($"Successfully UPDATED {countSuccess} of {dataGridView1.Rows.Count} rows");

从喜欢

我只想更新我只检查更新的列

标签: c#checkboxsql-updatemultiple-columns

解决方案


推荐阅读