2015-09-06 76 views
2

我可以正確地更新與datatable一個SQL數據庫表如下:使用的TableAdapter來更新數據庫表與一個DataTable

Dim resultsDataTable As New DataTable() 
Dim dtpHExportDataTable As New DataTable() 
Dim cnString As String = <<<ConnectionString>>> 
Using cnSQL1 As New SqlConnection 
    cnSQL1.ConnectionString = cnString 
     Using adapter1 = New SqlDataAdapter("SELECT SampleNo, Results, Complete_Date, Dex_Row_Id " & "FROM LIMS.dbo.Analytical_Sample_Log_ResultsInfo", cnSQL1) 
     Dim builder1 As New SqlCommandBuilder(adapter1) 
     adapter1.UpdateCommand = builder1.GetUpdateCommand() 
     Using New SqlCommandBuilder(adapter1) 
      adapter1.Fill(resultsDataTable) 
      resultsDataTable.PrimaryKey = New DataColumn() {resultsDataTable.Columns("Dex_Row_Id")} 
      dtpHExportDataTable = resultsDataTable.Clone() 
      AddResultsRow(dtpHExportDataTable, 13581, "4.4", "2015-01-01", 45598) 
      AddResultsRow(dtpHExportDataTable, 13590, "5.5", "2015-01-01", 45618) 
      AddResultsRow(dtpHExportDataTable, 13604, "6.6", "2015-01-01", 45655) 
      resultsDataTable.Merge(dtpHExportDataTable) 
      ShowResult(resultsDataTable) ‘looks perfect 
      adapter1.Update(resultsDataTable) ‘database table IS updated correctly 
     End Using 
    End Using 
End Using 

然而,當我在上面的代碼擴展到我的電子表格工作簿頁面應用程序中,resultsDataTable (由DevExpress ShowResult(resultsDataTable)顯示)使用上面的代碼以及下面的代碼顯示正確的信息。但數據庫表不使用以下代碼更新:

Dim worksheet As Worksheet = SpreadsheetControl.Document.Worksheets.ActiveWorksheet 
    Dim range As Range = worksheet.Selection 
    Dim rangeHasHeaders As Boolean = True 
    ' Create a data table with column names obtained from the first row in a range if it has headers. 
    ' Column data types are obtained from cell value types of cells in the first data row of the worksheet range. 
    Dim resultsDataTable As New DataTable() 
    Dim dtpHExportDataTable As DataTable = worksheet.CreateDataTable(range, rangeHasHeaders) 
    Dim cnString As String = "<<<ConnectionString>>>" 
    Using cnSQL1 As New SqlConnection 
     cnSQL1.ConnectionString = cnString   
    Using adapter1 = New SqlDataAdapter("SELECT SampleNo, Results, Complete_Date, Dex_Row_Id " & "FROM LIMS.dbo.Analytical_Sample_Log_ResultsInfo", cnSQL1) 
     Dim builder1 As New SqlCommandBuilder(adapter1) 
     adapter1.UpdateCommand = builder1.GetUpdateCommand() 
     Using New SqlCommandBuilder(adapter1) 
      adapter1.Fill(resultsDataTable) 
      resultsDataTable.PrimaryKey = New DataColumn() {resultsDataTable.Columns("Dex_Row_Id")} 
      dtpHExportDataTable = resultsDataTable.Clone() 
      Dim exporter As DataTableExporter = worksheet.CreateDataTableExporter(range, dtpHExportDataTable, rangeHasHeaders) 
      ' Perform the export. 
      exporter.Export() 
      resultsDataTable.Merge(dtpHExportDataTable) 
         ShowResult(resultsDataTable) ‘looks perfect 
      adapter1.Update(resultsDataTable)) ‘database table is NOT updated 
     End Using 
    End Using 
End Using 
+0

我不確定'worksheet.CreateDataTableExporter'做了什麼,但可能是它使用原始DataRow版本,因此它看不到更改。嘗試在'adapter1.Update'後面導出數據。 –

+0

代理,工作表.CreateDataTableExporter是一個DevExpress.com擴展,它從電子表格的指定範圍內獲取數據,並根據需要跳過標題行並填充指定的數據表。嘗試在adapter1.Update之後導出數據不起作用。 – TCIslandTime

+0

我猜Merge()沒有設置合併行的行狀態添加...所以你的resultsDataTable沒有變化,你的Update()什麼都不做...... – PrfctByDsgn

回答

0

假設您的代碼更新數據庫正在工作。並伴隨出口更新未處理。

然後問題是暴露於另一個過程後。更新不會經歷。

所以你可以這樣做。

Using adapter1 = New SqlDataAdapter("SELECT SampleNo, Results, Complete_Date, Dex_Row_Id " & "FROM LIMS.dbo.Analytical_Sample_Log_ResultsInfo", cnSQL1) 
    Dim builder1 As New SqlCommandBuilder(adapter1) 
    adapter1.UpdateCommand = builder1.GetUpdateCommand() 
    Using New SqlCommandBuilder(adapter1) 
     adapter1.Fill(resultsDataTable) 
     resultsDataTable.PrimaryKey = New DataColumn() {resultsDataTable.Columns("Dex_Row_Id")} 
     dtpHExportDataTable = resultsDataTable.Clone() 

     resultsDataTable.Merge(dtpHExportDataTable) 
        ShowResult(resultsDataTable) 
     adapter1.Update(resultsDataTable) 

     Dim exporter As DataTableExporter = worksheet.CreateDataTableExporter(range, dtpHExportDataTable, rangeHasHeaders) 
     ' Perform the export. 
     exporter.Export()) 

    End Using 
+0

這個更新在2500秒內正確行。我在存儲過程中將該數據表作爲參數傳遞 – TCIslandTime

0

我最終將數據表作爲參數傳遞給存儲過程。

-- Create User-defined Table Type 

USE LIMS 
GO 

-- Create the data type 
CREATE TYPE [dbo].[resultTable] AS TABLE 
(
[SampleNo]  [int]   NOT NULL, 
[PAprojid]  [nchar]  (10) NULL, 
[PAprojName]  [nchar]  (100) NULL, 
[STMTNAME]  [nchar]  (85) NULL, 
[DateAndTime]  [date]   NULL, 
[TestId1a]  [nchar]  (3) NULL, 
[TestType1a]  [nvarchar] (30) NULL, 
[Facility1a]  [nchar]  (80) NULL, 
[Results]  [nchar]  (10) NULL, 
[AUnits1]  [nchar]  (15) NULL, 
[SampleDate1a] [date]   NULL, 
[Complete_Date]  [date]   NULL, 
[Comments1a]  [nchar]  (100) NULL, 
[Dex_Row_Id]  [int]   NOT NULL 

PRIMARY KEY (Dex_Row_Id) 
) 
GO 

USE [LIMS] 
GO 

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[usp_Update_TestResults] 
@tblResults resultTable READONLY 
AS 
BEGIN 
SET NOCOUNT ON; 

MERGE INTO LIMS.dbo.Analytical_Sample_Log_ResultsInfo P 
USING @tblResults S 
ON P.Dex_Row_Id = S.Dex_Row_Id 
WHEN MATCHED THEN 
UPDATE SET P.Results = S.Results; 
END 

    For Each row As DataRow In resultsDataTable1.Rows 
       row.SetModified() 
      Next 

      resultsDataTable1.Merge(dtTSSExportDataTable) 

      Using updateCommand As New SqlCommand("usp_Update_TestResults") 
       updateCommand.Connection = cnSQL1 
       updateCommand.CommandType = CommandType.StoredProcedure 
       updateCommand.Parameters.AddWithValue("@tblResults", resultsDataTable1) 
       cnSQL1.Open() 
       updateCommand.ExecuteNonQuery() 
       cnSQL1.Close() 
      End Using 
相關問題