[英]VB.Net SQL insert from datagrid
我當前正在構建一個應用程序,用戶通過該應用程序將數據輸入到網格中,然后將其插入到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
為每一行創建新實例,我認為您的對象每次都添加相同的參數。
sqlRI = new sqlRI();//create new instance everytime
sqlRI.CommandText = "dbo.vb_insert"
sqlRI.CommandType = CommandType.StoredProcedure
sqlRI.Parameters.Add("@accid", SqlDbType.VarChar).Value = accid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.