1

我是新来的,只注册了,因为我迫切需要解决我的问题。.MDF我有一个 VB.NET 项目,该项目正在使用我已导入项目的本地文件。主键完好无损,所以我知道这不是我的问题。

我正在使用 Linq-to-SQL 查询数据库并进行更改。在我的主窗体上,我使用由查询填充的数据网格视图。我在插入时遇到问题,其中查询工作正常并且 DGV 使用新创建的数据进行更新,但是当我调用时SubmitChanges(),更改不会保存到实际的物理数据库中。我不确定我错过了什么或做错了什么。

这是我的代码,可以更好地帮助您获得有根据的答案。

Public Class frmCell_Leader
    'Reference DataContext
    Private db As New MarquardtClassesDataContext()

    Private Sub frmCell_Leader_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        FillProductionGrid()
        PrepareInsertFields()
    End Sub

    Private Sub FillProductionGrid()
        ''''''''''''''''''''''''''''''''''
        'E.Salce Last Modified 11.29.2012
        'Comments: Working
        ''''''''''''''''''''''''''''''''''

        'Populate DGV
        Dim query = From marq In db.tblProductivities
                 Select
     marq.PartNum, marq.Produced, marq.Packed, marq.Issue, marq.StationNum, _
     marq.PlanDownTime, marq.UnplannedDownTime, marq.CurDate

        dgvProduction.DataSource = query
    End Sub
    Private Sub PrepareInsertFields()
        ''''''''''''''''''''''''''''''''''
        'E.Salce Last Modified 11.29.2012
        'Comments: Working
        ''''''''''''''''''''''''''''''''''

        'Fill Planned Downtime Combo Box
        Dim PlannedQuery = From planned In db.tblPlannedDownTimes
                           Select planned.Plan_ID, planned.Description, planned.Active
                           Where Active = True

        cboPlanned.DataSource = PlannedQuery
        cboPlanned.DisplayMember = "Description"
        cboPlanned.ValueMember = "Plan_ID"

        'Fill UnPlanned Downtime Combo Box
        Dim UnPlannedQuery = From unplanned In db.tblUnplannedDownTimes
                           Select unplanned.Unplan_ID, unplanned.Description, unplanned.Active
                           Where Active = True

        cboUnplanned.DataSource = UnPlannedQuery
        cboUnplanned.DisplayMember = "Description"
        cboUnplanned.ValueMember = "UnPlan_ID"
    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        'Add the new Production to database
        'Create an productivity object
        Dim marqpro As New tblProductivity With {
            .PartNum = CShort(txtPartNumber.Text),
            .Produced = CShort(txtProduced.Text),
            .Packed = CShort(txtPacked.Text),
            .Issue = txtIssue.Text,
            .PlanDownTime = CShort(cboPlanned.SelectedValue),
            .UnplannedDownTime = CShort(cboUnplanned.SelectedValue),
            .CurDate = Date.Today}

        'insert newly created data
        Try
            'Submit changes too database
             db.tblProductivities.InsertOnSubmit(marqpro)

            db.SubmitChanges()
            db.Refresh(Data.Linq.RefreshMode.KeepCurrentValues, marqpro)

            'Refresh DGV
            FillProductionGrid()
            MessageBox.Show("Data added successfully")

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message)
        End Try
    End Sub
End Class
4

1 回答 1

1

也许您的项目中有一个 .mdf 文件,每次构建时都会将其复制到 bin/debug 文件夹中。

为什么我的 SubmitChanges() 不能在 LINQ-to-SQL 中工作?

于 2012-12-02T19:38:08.187 回答