VB.NET从数据视图网格将数据插入数据库

时间:2017-09-08 05:47:59

标签: sql-server vb.net

我想问一下,当插入来自数据网格的所有数据时,我可以提高速度。

例如:

我目前需要10,000个数据才能插入数据库sql server。 现在,我想将所有数据插入到数据库中。 但我觉得它会很慢。 我可以不使用for循环吗?

    Dim chk_cmd As SqlCommand
    Dim chk_con As SqlConnection
    Dim checker As SqlDataReader
    Dim chkint As Integer = 0
    Dim constring As String = "Data Source=exmag\sqlexpress;Initial Catalog=Stock;Integrated Security=true;"
    Try
        For Each row As DataGridViewRow In DataGridView1.Rows

            chk_con = New SqlConnection(constring)

            chk_con.Open()
            chk_cmd = New SqlCommand("SELECT stockId FROM stock WHERE stockId = '" & row.Cells("stockId").Value & "'", chk_con)
            checker = chk_cmd.ExecuteReader(CommandBehavior.CloseConnection)
            If checker.HasRows Then
                chkint += 1
            Else
                Using con_insert As New SqlConnection(constring), cmd_insert As New SqlCommand("INSERT INTO stock VALUES(@stockId,@id_android,@itemCode,@quantity)", con_insert)
                    cmd_insert.Parameters.AddWithValue("@stockId", row.Cells("stockId").Value)
                    cmd_insert.Parameters.AddWithValue("@id_android", row.Cells("id_android").Value)
                    cmd_insert.Parameters.AddWithValue("@itemCode", row.Cells("itemCode").Value)
                    cmd_insert.Parameters.AddWithValue("@quantity", row.Cells("quantity").Value)
                    con_insert.Open()
                    cmd_insert.ExecuteNonQuery()
                    con_insert.Close()
                End Using
            End If


        Next
    Catch ex As SqlException
        If ex.Number.Equals(2627) Then
            MsgBox("Primary Key DUPLICATED/Some Data are currently in the table.")
        End If
    End Try

    MsgBox(chkint & " Data has duplicated primary key!")

2 个答案:

答案 0 :(得分:0)

这是实现这一目标的一种方式;我用它来传输电子表格。 我同意jmcilhinney在网格中获取10000条记录毫无意义。

// create a datatable
Dim dt_eff As New DataTable
dt_eff.Columns.Add("Element", GetType(String))
dt_eff.Columns.Add("Eff", GetType(Decimal))
dt_eff.PrimaryKey = New DataColumn() {dt_eff.Columns("Element")}

// fill the datatable
For row As Integer = 4 To 50
    newRow = dt_eff.NewRow
    newRow("Element") = wsComp.Cells(row, 1).Value.ToString
    newRow("Eff") = CDec(wsComp.Cells(row, 6).Value)
    dt_eff.Rows.Add(newRow)
Next

// send the datatable to a sp on SQL SERVER
Dim param As SqlClient.SqlParameter
Using selectcmd As New SqlClient.SqlCommand("[dbo].[LoadingTable_IntegrateIncoming]", trans.Connection, trans)
    selectcmd.CommandType = CommandType.StoredProcedure
    param = selectcmd.Parameters.AddWithValue("@Efficiencies", dt_eff)
    param.SqlDbType = SqlDbType.Structured 
    selectcmd.ExecuteNonQuery()
End Using

// on the server side: declare a table type
CREATE TYPE [dbo].[TVP_Efficiencies] AS TABLE(
    [Element] [nvarchar](5) NOT NULL,
    [Eff] [decimal](18, 4) NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [Element] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)

// and create a stored proc that consumes the datatable
CREATE PROCEDURE [dbo].[LoadingTable_IntegrateIncoming] 
    @Efficiencies TVP_Efficiencies readonly
AS
BEGIN
    SET NOCOUNT ON;
    insert into efficiencies
    select * from @Efficiencies
END

答案 1 :(得分:0)

您可以使用DataTableDataAdapter

更新数据库

以下简要介绍。

    // con = SqlConnection '
    // queryString = your SELECT query 
    Using da As New SqlDataAdapter()
        da.SelectCommand = New SqlCommand( _
        queryString, connection)
        Dim dt As New DataTable("TABLE_NAME")
        da.Fill(dt)
        // Loop through your collection 
        // Add new Row to datatable 
        // Populate new row 
        Dim drow As DataRow = dt.NewRow()
        drow("Example Field") = "Example String"
        // Update database 
        dt.Add(drow)
        da.update(dt)
    End Using

N.B。使用双斜杠代替撇号进行注释,因为SO似乎突然用撇号突出显示语法。