首页 > 解决方案 > 如何从 datagridview 和文本框更新表格

问题描述

我使用此代码将数据从 datagridview 更新到我的 sql server 表中,但我没有收到任何错误,并且数据没有更新,所以我的代码中的问题在哪里。

连接也是正确的。

  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 server 表:

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

编辑

我使用来自@Caius Jard 的代码,但在 com.parameters 日期中出现错误“对象无法从 DBNull 转换为其他类型。”

这段代码

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" );

    }
}

标签: c#winformsdatagridviewupdates

解决方案


你的陈述

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

推荐阅读