将所有数据从DatagridView保存到数据库 [英] saving all Data from DatagridView to Database
本文介绍了将所有数据从DatagridView保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在vb.net中有一个Datagrid.
我要在按钮单击上添加行,然后根据在文本框中输入的数据填充行
如
I have a Datagrid in vb.net.
I am adding rows on button click and Filling the rows from data entered in text boxes
as
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim c As Integer
c = dgv.Rows.Count
dgv.Rows.Add()
dgv.Rows(c).Cells(0).Value = lblTID.Text.ToString()
dgv.Rows(c).Cells(1).Value = txtSlNo.Text.ToString()
dgv.Rows(c).Cells(2).Value = cmbItem.Text.ToString()
dgv.Rows(c).Cells(3).Value = txtQty.Text.ToString()
dgv.Rows(c).Cells(4).Value = txtDesc.Text.ToString()
End Sub
接下来,我有一个保存按钮,将Datagridview中的数据保存到databse中,我尝试了以下
Next I have save button to save the Data from Datagridview to databse I have tried the following
With MyCommand
For Each row As DataGridViewRow In DataGridView1.Rows
MyParam(0) = CreateParam("@ReturnValue", SqlDbType.Int, ParameterDirection.ReturnValue, 0, 0)
MyParam(1) = CreateParam("@TID", SqlDbType.Int, ParameterDirection.Input, 4, tid)
MyParam(2) = CreateParam("@SlNo", SqlDbType.Int, ParameterDirection.Input, 4, txtSlNo.Text)
MyParam(3) = CreateParam("@ItemID", SqlDbType.Int, ParameterDirection.Input, 20, cmbItem.SelectedIndex)
MyParam(4) = CreateParam("@ItemCode", SqlDbType.VarChar, ParameterDirection.Input, 20, txtItCode.Text)
MyParam(5) = CreateParam("@Qty", SqlDbType.Int, ParameterDirection.Input, 20, txtQty.Text)
MyParam(6) = CreateParam("@Descrip", SqlDbType.VarChar, ParameterDirection.Input, 20, txtDesc.Text)
.Parameters.Clear()
Dim i As Integer
For i = 0 To 6
.Parameters.Insert(i, MyParam(i))
Next
.CommandText = "SPIS_UpdateItemTransDtl"
MyConnection.ExecuteCommand(MyCommand)
lngErrorCode = CInt(.Parameters(0).Value)
If lngErrorCode < 0 Then
sbErrorMsg(lngErrorCode)
End If
Next
End With
但这只会插入所选行的值..
我希望一次插入所有数据.
assistant
But this only inserting the values of row which is selected..
I want all the Data to be inserted at once.
assist
推荐答案
如果您写过:
If you wrote:
For Each row As DataGridViewRow In DataGridView1.Rows
然后使用行值作为参数,而不是文本框值,例如:
then use row values for parameters, not textbox values, for example:
MyParam(1) = CreateParam("@TID", SqlDbType.Int, ParameterDirection.Input, 4, row.Cells(0).value)
...
...
这篇关于将所有数据从DatagridView保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文