将gridview中的数据插入数据库

时间:2016-04-19 08:29:15

标签: vb.net

使用SQL Server 2014,VS 2012,VB,n-layer

我将CSV文件中的数据导入gridview,然后将其插入到SQL Server数据库的临时表中。

所有这些都很好,如下面的代码所示,但有一个主要缺点。代码循环遍历gridview中的行,并一次从gridview中插入一行数据,这意味着它将打开与数据库的连接,插入行并关闭连接。

如果我在gridview中有100行数据,这意味着它将打开和关闭与数据库的连接100次,这似乎是一种非常低效的处理方式。我真正需要做的是批量插入所有行,只有一个打开和关闭操作。

这是我遇到问题的地方。我是否将行加载到数据表中?然后如何将数据表的内容传递到我的BLL和DAL层?

提前感谢任何建议。

UI:

Dim myTemporaryPatient As TemporaryPatient
myTemporaryPatient = New TemporaryPatient

For i As Integer = 0 To grdQuad.Rows.Count - 1
myTemporaryPatient.TempDEPM = grdQuad.Rows(i).Cells(0).Text

myTemporaryPatient.TempDateRecordAdded = Now()
myTemporaryPatient.TempPIDSurname = grdQuad.Rows(i).Cells(1).Text
myTemporaryPatient.TempPIDForename = grdQuad.Rows(i).Cells(2).Text
myTemporaryPatient.TempPIDNHSNo = grdQuad.Rows(i).Cells(3).Text
myTemporaryPatient.TempPIDDoB = grdQuad.Rows(i).Cells(4).Text
myTemporaryPatient.TempResult = grdQuad.Rows(i).Cells(5).Text
myTemporaryPatient.TempHospital = grdQuad.Rows(i).Cells(6).Text
myTemporaryPatient.TempDateOfResult = grdQuad.Rows(i).Cells(7).Text
myTemporaryPatient.TempLRisk = grdQuad.Rows(i).Cells(8).Text
myTemporaryPatient.TempRisk=GetNullableInt(grdQuad.Rows(i).Cells(9).Text)
myTemporaryPatient.TempLTrisomy18 = grdQuad.Rows(i).Cells(10).Text
myTemporaryPatient.TempTrisomy18=GetNullableInt(grdQuad.Rows(i).Cells(11).Text)
myTemporaryPatient.TempTestType = "Quad"

TemporaryPatientManager.InsertTemporaryPatient(myTemporaryPatient)

Next

DAL:

Public Shared Function InsertTemporaryPatient(ByVal myTemporaryPatient As LettersBusinessEntities.TemporaryPatient) As Integer

    Dim myTemporaryPatientRecord As LettersBusinessEntities.TemporaryPatient = Nothing
    Using myConnection As New SqlConnection(ApplicationConfiguration.ConnectionString)
        Using myCommand As New SqlCommand("SPInsertIntoTemporaryPatient", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            myCommand.Parameters.AddWithValue("@TempDEPM", myTemporaryPatient.TempDEPM)
            myCommand.Parameters.AddWithValue("@TempDateRecordAdded", myTemporaryPatient.TempDateRecordAdded)
            myCommand.Parameters.AddWithValue("@TempPIDSurname", myTemporaryPatient.TempPIDSurname)
            myCommand.Parameters.AddWithValue("@TempPIDForename", myTemporaryPatient.TempPIDForename)
            myCommand.Parameters.AddWithValue("@TempPIDNHSNo", myTemporaryPatient.TempPIDNHSNo)
            myCommand.Parameters.AddWithValue("@TempPIDDoB", myTemporaryPatient.TempPIDDoB)
            myCommand.Parameters.AddWithValue("@TempResult", myTemporaryPatient.TempResult)
            myCommand.Parameters.AddWithValue("@TempHospital", myTemporaryPatient.TempHospital)
            myCommand.Parameters.AddWithValue("@TempLRisk", myTemporaryPatient.TempLRisk)
            myCommand.Parameters.AddWithValue("@TempRisk", myTemporaryPatient.TempRisk)
            myCommand.Parameters.AddWithValue("@TempLTrisomy18", myTemporaryPatient.TempLTrisomy18)
            myCommand.Parameters.AddWithValue("@TempTrisomy18", myTemporaryPatient.TempTrisomy18)
            myCommand.Parameters.AddWithValue("@TempTestType", myTemporaryPatient.TempTestType)

            myConnection.Open()
            myCommand.ExecuteNonQuery()

        End Using
        myConnection.Close()

    End Using
    Return Nothing
End Function

0 个答案:

没有答案