使用TableAdapter使用DataTable更新数据库表

时间:2015-09-06 16:25:55

标签: vb.net ado.net

我可以使用datatable正确更新SQL数据库表,如下所示:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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