Linq-to-SQL SubmitChanges不更新物理数据库

时间:2012-12-01 21:06:31

标签: database vb.net linq-to-sql submitchanges

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

我使用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

1 个答案:

答案 0 :(得分:1)

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

Why isn't my SubmitChanges() working in LINQ-to-SQL?