变量名"@Loan_id"已经声明.变量名称在查询批处理或存储过程中必须唯一. [英] The variable name '@Loan_id' has already been declared. Variable names must be unique within a query batch or stored procedure.

查看:155
本文介绍了变量名"@Loan_id"已经声明.变量名称在查询批处理或存储过程中必须唯一.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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




        }



添加了代码块[/编辑]



Code block added[/Edit]

推荐答案

您需要在循环内发出一件事的查询,并且每次都需要清除参数循环.
You need to issue the query inside the loop for one thing, and you need to clear the parameters each time round the loop.
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");


这篇关于变量名"@Loan_id"已经声明.变量名称在查询批处理或存储过程中必须唯一.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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