参数化查询'((@Loan_id nvarchar(7),@ Date nvarchar(4000),@ Amount nvarchar(4000')需要参数'@Date',但未提供. [英] The parameterized query '(@Loan_id nvarchar(7),@Date nvarchar(4000),@Amount nvarchar(4000' expects the parameter '@Date', which was not supplied.

查看:187
本文介绍了参数化查询'((@Loan_id nvarchar(7),@ Date nvarchar(4000),@ Amount nvarchar(4000')需要参数'@Date',但未提供.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String query = "insert into Installments(Loan_id,Date,Amount)values(@Loan_id,@Date,@Amount)";
                        SqlCommand cmd = new SqlCommand(query, cn);
                        cmd.Prepare();

                        for (i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@Loan_id", txtloanidofinstallmentform.Text);
                            cmd.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells["Date"].Value);
                            cmd.Parameters.AddWithValue("@Amount", dataGridView1.Rows[i].Cells["Amount"].Value);
                        }

                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Record Stored Successfully", "Success Message", MessageBoxButtons.OK, MessageBoxIcon.Information);




                        }



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

您需要在for循环内而不是外部调用ExecuteNonQuery方法:
You need to invoke the ExecuteNonQuery method inside the for loop, not outside:
for (i = 0; i < dataGridView1.Rows.Count; i++)
{
           cmd.Parameters.Clear();
           cmd.Parameters.AddWithValue("@Loan_id", txtloanidofinstallmentform.Text);
           cmd.Parameters.AddWithValue("@Date",  dataGridView1.Rows[i].Cells["Date"].Value);
           cmd.Parameters.AddWithValue("@Amount", dataGridView1.Rows[i].Cells["Amount"].Value);
           cmd.ExecuteNonQuery(); // invoke ExecuteNonQuery inside the loop, not outside
}


希望这会有所帮助.


Hope this helps.


这篇关于参数化查询'((@Loan_id nvarchar(7),@ Date nvarchar(4000),@ Amount nvarchar(4000')需要参数'@Date',但未提供.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆