如何从 datagridview 和文本框更新 table

how update table from datagridview and textbox

我使用此代码将数据从 datagridview 更新到我的 sql 服务器 table 但是我没有收到任何错误并且数据没有更新所以我的代码中的问题在哪里.

连接也正确。

  using (SqlConnection con = new SqlConnection("**"))
        {
            con.Open();


            using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date 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.Add("@Payment_Date", SqlDbType.Date);
                com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
                SqlParameter SqlParameter = new SqlParameter("@collected", SqlDbType.Decimal);
                SqlParameter.SourceColumn = "collected";
                SqlParameter.Precision = 18;
                SqlParameter.Scale = 3;
                com.Parameters.Add(SqlParameter);

                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {

                    com.Parameters["@Subscriber_No"].Value = dataGridView1.Rows[i].Cells[0].Value;
                    com.Parameters["@collected"].Value = dataGridView1.Rows[i].Cells[1].Value;
                    com.Parameters["@Payment_Date"].Value = dataGridView1.Rows[i].Cells[2].Value;
                }
                com.ExecuteNonQuery();

                MessageBox.Show("Successfully UPDATE....");

            }
        }

sql 服务器 table :

Subscriber_No = bigint 
collected = numeric(18, 3)
company_name = nvarchar(50)
indebtedness_name = nvarchar(50)
Payment_Date = date

编辑

我使用来自@Caius Jard 的这段代码,但我在 com.parameters 日期出现错误 'Object cannot be cast from DBNull to other types.'

此代码

using (SqlConnection con = new SqlConnection("**"))
{
    con.Open();


    using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date 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.Add("@Payment_Date", SqlDbType.Date);
        com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
        com.Parameters.Add(new SqlParameter("@collected", SqlDbType.Decimal) { Precision = 18, Scale = 3 } );

        int countSuccess = 0;
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {

            com.Parameters["@Subscriber_No"].Value = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
            com.Parameters["@collected"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
            com.Parameters["@Payment_Date"].Value = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value); //hope this is a date, not a string. If it's a string, parse it instead
            int numUpd = com.ExecuteNonQuery();
            countSuccess += numUpd;
        }


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

    }
}

你的陈述

com.ExecuteNonQuery();

在循环之外,因为你的查询只对网格视图中的最后一行执行,我假设你想使用循环更新所有行。

所以你必须为每一行

执行com
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    com.Parameters["@Subscriber_No"].Value = dataGridView1.Rows[i].Cells[0].Value;
    com.Parameters["@collected"].Value = dataGridView1.Rows[i].Cells[1].Value;
    com.Parameters["@Payment_Date"].Value = dataGridView1.Rows[i].Cells[2].Value;
    com.ExecuteNonQuery();
}
    using (SqlConnection con = new SqlConnection("**"))
    {
        con.Open();


        using (SqlCommand com = new SqlCommand("UPDATE indebtedness SET collected=@collected,Payment_Date=@Payment_Date 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.Add("@Payment_Date", SqlDbType.Date);
            com.Parameters.Add("@Subscriber_No", SqlDbType.BigInt);
            com.Parameters.Add(new SqlParameter("@collected", SqlDbType.Decimal) { Precision = 18, Scale = 3 } );

            int countSuccess = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {

                com.Parameters["@Subscriber_No"].Value = Convert.ToInt64(dataGridView1.Rows[i].Cells[0].Value);
                com.Parameters["@collected"].Value = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
                com.Parameters["@Payment_Date"].Value = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value); //hope this is a date, not a string. If it's a string, parse it instead
                int numUpd = com.ExecuteNonQuery();
                countSuccess += numUpd;
            }


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

        }
    }

如果您看到 "updated 0..",那么您的搜索子句有问题(没有包含 company_name 和 indebtedness_name 的行)

如果您看到 "updated X",其中 X 大于 0,则更新已发送到数据库;检查你在寻找正确的数据库

请注意,除非有一个触发器在后台执行某些操作,否则此代码几乎没有意义 - 它所做的只是用新值一遍又一遍地更新相同的(一组)行,但是很多线在网格中。最终的静止状态就好像你 运行 只有最后更新行一样,所以如果你的网格有 500 行就没有意义,在 运行 前 499 次更新中(除非触发器正在捕获每一个变化并且用它做一些有趣的事情)