VB.Net OLEDB ExecuteNonQuery INSERT INTO - 对数据库没有影响

时间:2016-08-19 08:06:54

标签: vb.net access oledbcommand

这是我的问题: 我有下面的代码。 如果我调试我的代码并将查询直接复制到MS Access中,则查询工作完全正常,但如果我从我的应用程序执行它,则不会对表进行任何更改。

请注意,与数据库的连接是正常的,因为我正在进行多次选择,之后才能正常运行。

我可能做了一些愚蠢的事情,但它太大了,经过几个小时的努力,我看不到它。

我知道我应该在查询中使用参数并且我最初做了但是我在很多尝试中改变它以使其工作并且我认为这不应该改变太多。

    Dim cmd As New OleDbCommand
    Dim sQuery As String = String.Empty

    Try
        cmd.CommandText = "DELETE * FROM tbl_Invoices"
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MsgBox("PrepareInvoicing Delete" & vbCrLf & ErrorToString())
        Exit Sub
    End Try

    'Insert into the Invoice table the fleet info with Usage for the invoicing period selected
    Try
        sQuery = String.Empty
        sQuery = sQuery & "INSERT INTO tbl_Invoices "
        sQuery = sQuery & "SELECT tbl_Fleet.CustomerName AS CustomerName, "
        sQuery = sQuery & "tbl_Fleet.CountryCode AS CountryCode, "
        sQuery = sQuery & "#" & DateSerial(InvoicingYear, InvoicingMonth + 1, 0) & "# AS InvoiceDate, "
        sQuery = sQuery & "tbl_Fleet.Area AS Area, "
        sQuery = sQuery & "tbl_Fleet.Group AS [Group], "
        sQuery = sQuery & "tbl_Fleet.Site_nm AS SiteName, "
        sQuery = sQuery & "tbl_Sites.RCS AS CustomerPO, "
        sQuery = sQuery & "tbl_Fleet.Site_Addr_1 AS SiteAddress1, "
        sQuery = sQuery & "tbl_Fleet.Site_Addr_2 AS SiteAddress2, "
        sQuery = sQuery & "tbl_Fleet.Site_Addr_ZIP AS ZIP, "
        sQuery = sQuery & "tbl_Fleet.Site_Addr_cty AS City, "
        sQuery = sQuery & "tbl_Fleet.model_nm AS ProductDescription, "
        sQuery = sQuery & "tbl_Fleet.product_no AS ProductNumber, "
        sQuery = sQuery & "tbl_Fleet.serial_no AS SerialNumber, "
        sQuery = sQuery & "tbl_Fleet.hostname AS hostname, "
        sQuery = sQuery & "tbl_Fleet.asset_no AS AssetNumber, "
        sQuery = sQuery & "tbl_Fleet.Grid AS Grid, "
        sQuery = sQuery & "tbl_Fleet.ChangeOrderID AS ChangeOrderID, "
        sQuery = sQuery & "tbl_Fleet.install_date AS InstalledDate, "
        sQuery = sQuery & "INT(((tbl_Fleet.install_date - temptbl_CO.ChangeOrderStartDate)/365.25)+1) AS YearInContract, "
        sQuery = sQuery & "(tbl_RM.BlackClicks + tbl_RM.AccentClicks) AS BlackPages, "
        sQuery = sQuery & "(tbl_RM.ColorClicks + tbl_RM.ProfessionalColorClicks) AS ColorPages "
        sQuery = sQuery & "FROM tbl_Fleet, "
        sQuery = sQuery & "tbl_Sites, "
        sQuery = sQuery & "tbl_RM, "
        sQuery = sQuery & "(SELECT DISTINCT tbl_Bases.ProductNumber, tbl_Bases.ChangeOrderID, tbl_Bases.ChangeOrderStartDate FROM tbl_Bases WHERE tbl_Bases.CustomerName = '" & mdlGlobalStuff.SelectedCustomerName & "' AND tbl_Bases.CountryCode = '" & mdlGlobalStuff.SelectedCountryCode & "')  AS temptbl_CO "
        sQuery = sQuery & "WHERE tbl_Fleet.CustomerName = '" & mdlGlobalStuff.SelectedCustomerName & "' "
        sQuery = sQuery & "AND tbl_Fleet.CountryCode = '" & mdlGlobalStuff.SelectedCountryCode & "' "
        sQuery = sQuery & "AND tbl_Fleet.LoadDate = #" & LoadFleetDate & "# "
        sQuery = sQuery & "AND MONTH(tbl_RM.RMDate) = " & Month(LoadUsageDate) & " "
        sQuery = sQuery & "AND YEAR(tbl_RM.RMDate) = " & Year(LoadUsageDate) & " "
        sQuery = sQuery & "AND tbl_Fleet.CustomerName = tbl_Sites.CustomerName "
        sQuery = sQuery & "AND tbl_Fleet.CountryCode = tbl_Sites.CountryCode "
        sQuery = sQuery & "AND tbl_Fleet.Site_nm = tbl_Sites.Site_nm "
        sQuery = sQuery & "AND tbl_Fleet.CustomerName = tbl_RM.CustomerName "
        sQuery = sQuery & "AND tbl_Fleet.CountryCode = tbl_RM.CountryCode  "
        sQuery = sQuery & "AND tbl_Fleet.serial_no = tbl_RM.SerialNumber "
        sQuery = sQuery & "AND tbl_Fleet.product_no = temptbl_CO.ProductNumber "
        sQuery = sQuery & "AND tbl_Fleet.ChangeOrderID = temptbl_CO.ChangeOrderID "
        cmd.CommandText = sQuery
        cmd.ExecuteNonQuery()
        MsgBox("Step 1")
    Catch ex As Exception
        MsgBox("PrepareInvoicing: Invoicing step 1" & vbCrLf & ErrorToString())
        Exit Sub
    End Try

    'Update the "non aging" Bases (if Base.contractYear = 0) in the Invoice table
    Try
        sQuery = String.Empty
        sQuery = sQuery & "UPDATE tbl_Invoices "
        sQuery = sQuery & "INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber  =  tbl_Invoices.ProductNumber "
        sQuery = sQuery & "AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID "
        sQuery = sQuery & "SET tbl_Invoices.Base = tbl_Bases.BasePrice "
        sQuery = sQuery & "WHERE tbl_Bases.CustomerName = '" & mdlGlobalStuff.SelectedCustomerName & "' "
        sQuery = sQuery & "AND tbl_Bases.CountryCode = '" & mdlGlobalStuff.SelectedCountryCode & "' "
        sQuery = sQuery & "AND tbl_Bases.ContractYear = 0 "
        cmd.CommandText = sQuery
        cmd.ExecuteNonQuery()
        MsgBox("Step 2")
    Catch ex As Exception
        MsgBox("PrepareInvoicing: Invoicing step 2" & vbCrLf & ErrorToString())
        Exit Sub
    End Try

    'Update the "aging" Bases (if Base.contractYear <> 0) in the Invoice table
    Try
        sQuery = String.Empty
        sQuery = sQuery & "UPDATE tbl_Invoices "
        sQuery = sQuery & "INNER JOIN tbl_Bases ON tbl_Bases.ProductNumber  =  tbl_Invoices.ProductNumber "
        sQuery = sQuery & "AND tbl_Bases.ChangeOrderID = tbl_Invoices.ChangeOrderID "
        sQuery = sQuery & "AND tbl_Bases.ContractYear =  tbl_Invoices.YearInContract  "
        sQuery = sQuery & "SET tbl_Invoices.Base = tbl_Bases.BasePrice "
        sQuery = sQuery & "WHERE tbl_Bases.CustomerName = '" & mdlGlobalStuff.SelectedCustomerName & "' "
        sQuery = sQuery & "AND tbl_Bases.CountryCode = '" & mdlGlobalStuff.SelectedCountryCode & "' "
        cmd.CommandText = sQuery
        cmd.ExecuteNonQuery()
        MsgBox("Done")
    Catch ex As Exception
        MsgBox("PrepareInvoicing: Invoicing step 3" & vbCrLf & ErrorToString())
        Exit Sub
    End Try

1 个答案:

答案 0 :(得分:2)

我会继续将此作为答案发布,因为根据我的经验,它至少有95%可能适用。

添加本地数据文件时,例如MDB或ACCDB文件,对于您的项目,它将与所有其他源文件一起复制到项目文件夹中。该文件是项目的一部分,而不是您的应用程序的一部分。任何架构更改或默认数据都会添加到该文件中,但在测试时不会在运行时触及它。

构建项目时,该源文件将与EXE一起复制到输出文件夹。它是您的应用程序在运行时使用的副本。您保存的任何数据都会保存到该工作副本,而不是源文件。

默认情况下,会生成源文件的新副本,并在每次构建时覆盖工作副本。这意味着,如果您在调试器中运行应用程序,保存一些数据,停止应用程序,更改代码然后再次运行应用程序,您保存的数据将会消失。

所以,你可能犯的错误,就像许多人一样,是你要么在源文件中查找你在运行时保存的数据,要么就是在查看工作副本被下一个构建覆盖后。解决这个&#34;问题&#34;很简单。在Solution Explorer中选择您的数据文件,打开Properties窗口并将 groupsCountLbl.frame = CGRectMake(groupsLbl.frame.maxX + 5, seperatorLbl.frame.maxY + 15 , 40, 20) groupsCountLbl.sizeToFit() table.frame = CGRectMake(0, groupsCountLbl.frame.maxY + 10, bounds.width, bounds.height - groupsCountLbl.frame.maxY + 10) scrollView.contentSize = CGSizeMake(bounds.width, bounds.height + self.table.frame.height) scrollView.scrollEnabled = true 属性设置为Copy to Output Directory。这意味着,在构建时,如果源文件较新,则只会覆盖工作副本,如果您已修改架构或编辑了默认数据,则会出现这种情况。如果您需要刷新工作数据库,只需从输出文件夹中手动删除它,或暂时将Copy if Newer设置回Copy to Output Directory

你可能想知道为什么他们首先使用多个文件,但它完全合乎逻辑,实际上是一件非常好的事情。如果您只有一个文件并且用它进行测试,那么在部署时会发生什么?你不得不浪费时间清理那个文件,然后你可能会遗漏一些东西。这样,您只需继续使用Debug副本进行测试,并且在执行Release版本时,您将始终获得一个干净的数据文件。