c# - 如何从 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" );
}
}
解决方案
你的陈述
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();
}
推荐阅读
- java - 在 Spring Boot 应用程序启动期间初始化静态变量后未设置
- java - 无法让 Spring 引导端点同时支持 XML 和 JSON 请求和响应
- html - 为什么访问的链接会禁用 svg 图标的填充属性的转换?
- reactjs - NextJS 中的 createPortal
- git - Git远程获取然后结帐时会发生大量文件更改
- python - 删除 Python 列表中的前导/尾随停用词
- xml - 在类型值上创建 XPath 过滤?
- javascript - 通过 Babel Loader 运行 Babel 时的行为不一致
- python - Python中是否有一种快速的方法可以按列和行分组来拆分?
- c# - 重新抛出 JsonConverter 异常的正确方法