Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用TableAdapter使用DataTable更新数据库表_Vb.net_Ado.net - Fatal编程技术网

Vb.net 使用TableAdapter使用DataTable更新数据库表

Vb.net 使用TableAdapter使用DataTable更新数据库表,vb.net,ado.net,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 = cnStr

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

假设您更新数据库的代码正在运行。未处理与导出更新相关的

那么问题是在暴露于另一个进程之后。不进行更新

所以你可以这样做

    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

最后,我将数据表作为参数在存储过程中传递

-- 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

我不确定
工作表.CreateDataTableExporter
做什么,但它可能使用原始DataRow版本,因此看不到更改。尝试在
adapter1.Update
.agent之后导出数据,sheet.CreateDataTableExporter是一个DevExpress.com扩展,它从电子表格中获取指定范围的数据,如果需要,它将跳过标题行,并填充指定的数据表。尝试在adapter1.Update之后导出数据无效。我猜Merge()没有将合并行的rowstate设置为added。。。因此,resultsDataTable中没有任何更改,而Update()什么也不做……这将在一秒钟内正确更新2500行。我已将datatable作为存储过程中的参数传递
-- 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