0

現在、ユーザーがデータをグリッドに入力し、これを SQL テーブルに挿入するアプリケーションを構築しています。ただし、一番上の行よりも多く挿入するのに問題があります。プロシージャに対して宣言されているパラメータが多すぎるというエラーが表示されます。行ごとに挿入が発生するループを実行しようとしています。実行すると一番上の行が挿入されますが、それ以外は無視されます

    For i = 0 To dgvRI.Rows.Count - 1
            sqlRI.CommandText = "dbo.vb_insert"
            sqlRI.CommandType = CommandType.StoredProcedure
            sqlRI.Parameters.Add("@accid", SqlDbType.VarChar).Value = accid
            sqlRI.Parameters.Add("@rnum", SqlDbType.Int).Value = dgvRI.Rows(i).Cells(0).Value
            sqlRI.Parameters.Add("@rilim", SqlDbType.Decimal).Value = dgvRI.Rows(i).Cells(1).Value
            sqlRI.Parameters.Add("@riatt", SqlDbType.Decimal).Value = dgvRI.Rows(i).Cells(2).Value
            sqlRI.Parameters.Add("@rishare", SqlDbType.Decimal).Value = dgvRI.Rows(i).Cells(3).Value
            sqlRI.Parameters.Add("@rname", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(4).Value
            sqlRI.Parameters.Add("@bname", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(5).Value
            sqlRI.Parameters.Add("@type", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(6).Value
            sqlRI.Parameters.Add("@cov", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(7).Value
            sqlRI.Parameters.Add("@prop", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(8).Value
            sqlRI.Parameters.Add("@loc", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(9).Value
            sqlRI.Parameters.Add("@con", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(10).Value
            sqlRI.Parameters.Add("@prem", SqlDbType.Decimal).Value = dgvRI.Rows(i).Cells(11).Value
            sqlRI.Parameters.Add("@curr", SqlDbType.VarChar).Value = dgvRI.Rows(i).Cells(12).Value
            i += sqlRI.ExecuteNonQuery()
        Next
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
4

1 に答える 1

0

行ごとに新しいインスタンスを作成します。オブジェクトは毎回同じパラメーターを追加していると思います。

        sqlRI = new sqlRI();//create new instance everytime
        sqlRI.CommandText = "dbo.vb_insert"
        sqlRI.CommandType = CommandType.StoredProcedure
        sqlRI.Parameters.Add("@accid", SqlDbType.VarChar).Value = accid
于 2012-11-08T16:53:35.343 に答える