I have a winform app (VB 2005) that allows users to export data to
excel, make updates to the excel file and import the data from that
Excel file and update the database.
My question is: Is it best to do it this way, calling the update
stored procedure for every update? Or should I be loading this data
into a staging table, and if all goes well do the 'Real' Update. Or
put this into a data adapter and update from that? The application
will never update a huge amout of records, maybe 500 at the most. But
I would think this wouldn't scale when I have 5000 Records.
<<Snip>>
Using connection As New SqlConnection(g _sRCT_Conn)
'Get UserID
Dim uid As New SqlCommand("dbo .spoc_Get_UserI D",
connection)
uid.CommandType = CommandType.Sto redProcedure
'Open conn
connection.Open ()
'Return Results
Dim uidResult As New SqlParameter("@ UserID",
SqlDbType.Int)
uidResult.Direc tion = ParameterDirect ion.Output
uid.Parameters. Add(uidResult)
'Input parms
Dim sUID As String = g_sCurrUserDoma in & "\" & g_sCurrUser
uid.Parameters. Add("@PrefID", SqlDbType.VarCh ar).Value =
sUID
'Exec and get user id
uid.ExecuteNonQ uery()
Dim uidID As Integer = CInt(uidResult. Value)
uid.Dispose()
'Get total records to update
.Range("H2").Se lect()
.Selection.End( Excel.XlDirecti on.xlDown).Sele ct()
Dim iTotalRecs As Integer = .ActiveCell.Row - 1
.Range("H2").Se lect()
'Loop thru RecordIDs and get notes
Dim dtUpdate As Date = Now()
Do While .ActiveCell.Tex t <""
If .ActiveCell.Off set(0, iNOTES_OFFSET). Text <""
Then
'Update records
Dim u As New SqlCommand
("dbo.spoc_ev_I mportUpdate", connection)
u.CommandType = CommandType.Sto redProcedure
'Return Value
Dim uResult As New SqlParameter("@ Result",
SqlDbType.Int)
uResult.Directi on = ParameterDirect ion.Output
u.Parameters.Ad d(uResult)
'Input Parms
u.Parameters.Ad d("@ItemID", SqlDbType.Int). Value =
CInt(.ActiveCel l.Text)
u.Parameters.Ad d("@UserID", SqlDbType.Int). Value =
uidID
u.Parameters.Ad d("@UpdateDate" ,
SqlDbType.DateT ime).Value = dtUpdate
u.Parameters.Ad d("@Notes",
SqlDbType.VarCh ar).Value = .ActiveCell.Off set(0, iNOTES_OFFSET). Text
'Update
u.ExecuteNonQue ry()
If Not CInt(uResult.Va lue) = 0 Then
MessageBox.Show ("Error updating RecordID "
& .ActiveCell.Tex t, _
g_sApp_Name, MessageBoxButto ns.OK,
MessageBoxIcon. Error)
iCount = -1
bError = True
Else
iCount += 1
Me.ssStatus.Tex t = "Updating ... " & iCount _
& " of " & iTotalRecs & " Records"
Application.DoE vents()
End If
End If
.ActiveCell.Off set(1, 0).Select()
Loop
End Using
<<Snip>>
excel, make updates to the excel file and import the data from that
Excel file and update the database.
My question is: Is it best to do it this way, calling the update
stored procedure for every update? Or should I be loading this data
into a staging table, and if all goes well do the 'Real' Update. Or
put this into a data adapter and update from that? The application
will never update a huge amout of records, maybe 500 at the most. But
I would think this wouldn't scale when I have 5000 Records.
<<Snip>>
Using connection As New SqlConnection(g _sRCT_Conn)
'Get UserID
Dim uid As New SqlCommand("dbo .spoc_Get_UserI D",
connection)
uid.CommandType = CommandType.Sto redProcedure
'Open conn
connection.Open ()
'Return Results
Dim uidResult As New SqlParameter("@ UserID",
SqlDbType.Int)
uidResult.Direc tion = ParameterDirect ion.Output
uid.Parameters. Add(uidResult)
'Input parms
Dim sUID As String = g_sCurrUserDoma in & "\" & g_sCurrUser
uid.Parameters. Add("@PrefID", SqlDbType.VarCh ar).Value =
sUID
'Exec and get user id
uid.ExecuteNonQ uery()
Dim uidID As Integer = CInt(uidResult. Value)
uid.Dispose()
'Get total records to update
.Range("H2").Se lect()
.Selection.End( Excel.XlDirecti on.xlDown).Sele ct()
Dim iTotalRecs As Integer = .ActiveCell.Row - 1
.Range("H2").Se lect()
'Loop thru RecordIDs and get notes
Dim dtUpdate As Date = Now()
Do While .ActiveCell.Tex t <""
If .ActiveCell.Off set(0, iNOTES_OFFSET). Text <""
Then
'Update records
Dim u As New SqlCommand
("dbo.spoc_ev_I mportUpdate", connection)
u.CommandType = CommandType.Sto redProcedure
'Return Value
Dim uResult As New SqlParameter("@ Result",
SqlDbType.Int)
uResult.Directi on = ParameterDirect ion.Output
u.Parameters.Ad d(uResult)
'Input Parms
u.Parameters.Ad d("@ItemID", SqlDbType.Int). Value =
CInt(.ActiveCel l.Text)
u.Parameters.Ad d("@UserID", SqlDbType.Int). Value =
uidID
u.Parameters.Ad d("@UpdateDate" ,
SqlDbType.DateT ime).Value = dtUpdate
u.Parameters.Ad d("@Notes",
SqlDbType.VarCh ar).Value = .ActiveCell.Off set(0, iNOTES_OFFSET). Text
'Update
u.ExecuteNonQue ry()
If Not CInt(uResult.Va lue) = 0 Then
MessageBox.Show ("Error updating RecordID "
& .ActiveCell.Tex t, _
g_sApp_Name, MessageBoxButto ns.OK,
MessageBoxIcon. Error)
iCount = -1
bError = True
Else
iCount += 1
Me.ssStatus.Tex t = "Updating ... " & iCount _
& " of " & iTotalRecs & " Records"
Application.DoE vents()
End If
End If
.ActiveCell.Off set(1, 0).Select()
Loop
End Using
<<Snip>>
Comment