2

我正在尝试在 VB.NET 中编写一个按钮处理程序,如果选中了复选框,它将从 gridview 读取行并将它们写入数据库。

我将此应用程序设置为使用 EntityDataSource 并将我的 .edmx 文件添加到 DAL 文件夹。我已经编写了按钮方法,但我对 EF 的了解不够,不知道如何处理来自 gridview 的数据。这是我的 btn_click 方法:

Private Sub btnGetChecks_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetChecks.Click
'********************************************
'* gets status of checkbox and writes to db *
'********************************************
Dim row As GridViewRow

Label6.Text = ""
For Each row In DvsGridView.Rows
  Dim RowCheckBox As CheckBox = CType(row.FindControl("chkStatus"), CheckBox)
  If RowCheckBox.Checked Then
    Label6.Text += row.Cells(5).Text & " Checked "
    ' Need to write checked data to the db
    ' ******* WHAT GOES HERE? *******
    Else

    Label6.Text += row.Cells(5).Text & " Unchecked "

  End If
Next
End Sub

我对 EDMX 相当陌生,但了解 VB.net。任何方向将不胜感激。

4

2 回答 2

1

检查此示例代码并修改它以匹配您的实体和对象

 Using db As New DBEntities()

   'set values here
    Dim x As New TableNameE()

    x.col1 = "value1"
    x.col2 = "value2"

    db.AddToTableNameE(x)
    db.SaveChanges()
End Using
于 2012-10-26T18:46:28.327 回答
0

感谢@rs.,我能够找出我的问题。参考已接受的答案,对于那些想知道您的项目中应该包含什么 .AddToTableNameE() 的人,它只是 .Add(x)。对每一行使用 .SaveChanges() 也会对性能造成一定影响,您应该创建一个 Dim List(Of TableNameE()) 并将“x”添加到列表中,然后像这样使用 .AddRange() :

//creating a data access class is one way to connect to your db by passing in db path
Dim context As New Namespace.YourContext(New DataAccessClass() With {.Path = aBrowsedForSDFFileTextBoxMaybe.Text})
Dim listOfTableObjects As List(Of Namespace.TableObject) = New List(Of Namespace.TableObject)

    For n = 1 To SomethingRelatedToRows
    Dim tableObject As New Namespace.TableObject() With {
                    .entityProp1 = TableObject(n).entityProp1,
                    .entityProp2 = TableObject(n).entityProp2,
                    .entityProp3 = TableObject(n).entityProp3,
                    .entityProp4 = TableObject(n).entityProp4,
                    .entityProp5 = TableObject(n).entityProp5,
                    .entityProp6 = TableObject(n).entityProp6
                }

        listOfTableObjects.Add(tableObject)

    Next n

    //.TableObjects being the DbSet from the ...Context.vb file
    context.TableObjects.AddRange(listOfTableObjects) 
    context.SaveChanges()
于 2016-05-11T00:02:28.323 回答