C
CPEdison
Greetings all,
This is a repost. I had to put this down for a while, but now I have to
get it running.
I am using an Access Database to store question information for a
questionnaire application. I am using VB.net as the front end, and
ADO.net to retrieve the information from the database. My data is bring
displayed in a DataGrid bound to a dataset. All of my calls to the
database are to parameter queries.
The problem that I am having is with the update command. When I call
the update method for my data adapter, and pass a dataset containing
just the changed records, only one record shows the change. I know that
parameter queries work because data is being returned and update, just
not in the quantities that I want.
From what I have read, the update method in ADO.NET should be able to
process a batch change, but the only specific information I have been
able to find concerns SQL Server and not MS Access. The only
information I have been able to find regarding the update method and MS
Access were for single records.
Does ADO.net send each update request separately, waiting for
conformations between requests, or does it send all of the changes at
once? I know, if you write the queries properly, SQL server can process
the data in a lump sum, but I do not believe Access has that ability.
Below is a copy of the offending code. Any help on this would be
greatly appreciated.
Thanks in advance,
Edison
Private Sub SaveData()
Dim lRetVal As Long
Dim oQuestions As Questions = New Questions
Dim oDS_Changes As DataSet = New DataSet
Dim sMsg As String
'Stop current edits
'Me.BindingContext(m_oDS).EndCurrentEdit()
'Grab all changed rows
oDS_Changes = m_oDS.GetChanges
'Check to see if any changes have been made
If (Not (oDS_Changes) Is Nothing) Then
sMsg = "Are you sure you want to save these " & _
oDS_Changes.Tables(0).Rows.Count() & " rows to the database?" _
lRetVal = MsgBox(sMsg, Microsoft.VisualBasic.MsgBoxStyle.Question + _
Microsoft.VisualBasic.MsgBoxStyle.YesNo, "Save Records")
Select Case lRetVal
Case vbYes
Try
' Save all changes
oQuestions.SaveData(oDS_Changes)
m_oDS.AcceptChanges()
Catch updateException As System.Exception
Throw updateException
Catch e As Exception
Throw e
End Try
Case vbNo
'Do nothing
End Select
End If
End Sub
Public Function SaveData(ByVal oDS As DataSet) As String
Dim strMsg As String
Dim lngRecsAffected As Long
Try
If (Not (oDS) Is Nothing) Then
'open connection to Database
m_oCn.Open()
'attempt to update the data source
da.Update(oDS, strClassName)
End If
Catch updateException As System.Exception
Throw updateException
Finally
'Close the Database Connection
m_oCn.Close()
End Try
End Function
Dim cmdSel As OleDbCommand
Dim cmdIns As OleDbCommand
Dim cmdUpd As OleDbCommand
Dim cmdDel As OleDbCommand
Dim strSQL As String
'Setup Select Command
strSQL = "QryQuestion"
cmdSel = New OleDbCommand(strSQL, m_oCn)
cmdSel.CommandType = CommandType.StoredProcedure
'Setup Insert Command
strSQL = "addqryQuestion"
cmdIns = New OleDbCommand(strSQL, m_oCn)
cmdIns.CommandType = CommandType.StoredProcedure
cmdIns.Parameters.Add("QuestionText", OleDbType.LongVarWChar,
255, "Question_Text")
cmdIns.Parameters.Add("CorrectAnswer", OleDbType.BigInt, 1,
"Correct_Answer")
cmdIns.Parameters.Add("QuestionNumber", OleDbType.BigInt, 1,
"Question_Number")
'Setup Update Command
strSQL = "updqryQuestion"
cmdUpd = New OleDbCommand(strSQL, m_oCn)
cmdUpd.CommandType = CommandType.StoredProcedure
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionNumber",
OleDbType.BigInt, 50, "Question_Number"))
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionText",
OleDbType.LongVarWChar, 255, "Question_Text"))
cmdUpd.Parameters.Add(New OleDbParameter("@CorrectAnswer",
OleDbType.BigInt, 50, "Correct_Answer"))
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionID",
OleDbType.BigInt, 50, "Question_ID")).SourceVersion =
DataRowVersion.Original
'Setup Delete Command
strSQL = "delqryQuestion"
cmdDel = New OleDbCommand(strSQL, m_oCn)
cmdDel.CommandType = CommandType.StoredProcedure
cmdDel.Parameters.Add("QuestionID", OleDbType.BigInt, 50,
"Question_ID")
initalizeDataAdapter(cmdSel, cmdIns, cmdUpd, cmdDel)
End Sub
Protected Sub initalizeDataAdapter(ByVal cmdSel As OleDbCommand, ByVal
cmdIns As OleDbCommand, ByVal cmdUpd As OleDbCommand, ByVal cmdDel As
OleDbCommand)
'Create and Setup Data Adapter
da = New OleDbDataAdapter
'Setup Select Command
da.SelectCommand = cmdSel
'Setup Insert Command
da.InsertCommand = cmdIns
'Setup Update Command
da.UpdateCommand = cmdUpd
'Setup Delete Command
da.DeleteCommand = cmdDel
'Destroy connection object
' m_oCn.Close()
End Sub
This is a repost. I had to put this down for a while, but now I have to
get it running.
I am using an Access Database to store question information for a
questionnaire application. I am using VB.net as the front end, and
ADO.net to retrieve the information from the database. My data is bring
displayed in a DataGrid bound to a dataset. All of my calls to the
database are to parameter queries.
The problem that I am having is with the update command. When I call
the update method for my data adapter, and pass a dataset containing
just the changed records, only one record shows the change. I know that
parameter queries work because data is being returned and update, just
not in the quantities that I want.
From what I have read, the update method in ADO.NET should be able to
process a batch change, but the only specific information I have been
able to find concerns SQL Server and not MS Access. The only
information I have been able to find regarding the update method and MS
Access were for single records.
Does ADO.net send each update request separately, waiting for
conformations between requests, or does it send all of the changes at
once? I know, if you write the queries properly, SQL server can process
the data in a lump sum, but I do not believe Access has that ability.
Below is a copy of the offending code. Any help on this would be
greatly appreciated.
Thanks in advance,
Edison
Private Sub SaveData()
Dim lRetVal As Long
Dim oQuestions As Questions = New Questions
Dim oDS_Changes As DataSet = New DataSet
Dim sMsg As String
'Stop current edits
'Me.BindingContext(m_oDS).EndCurrentEdit()
'Grab all changed rows
oDS_Changes = m_oDS.GetChanges
'Check to see if any changes have been made
If (Not (oDS_Changes) Is Nothing) Then
sMsg = "Are you sure you want to save these " & _
oDS_Changes.Tables(0).Rows.Count() & " rows to the database?" _
lRetVal = MsgBox(sMsg, Microsoft.VisualBasic.MsgBoxStyle.Question + _
Microsoft.VisualBasic.MsgBoxStyle.YesNo, "Save Records")
Select Case lRetVal
Case vbYes
Try
' Save all changes
oQuestions.SaveData(oDS_Changes)
m_oDS.AcceptChanges()
Catch updateException As System.Exception
Throw updateException
Catch e As Exception
Throw e
End Try
Case vbNo
'Do nothing
End Select
End If
End Sub
Public Function SaveData(ByVal oDS As DataSet) As String
Dim strMsg As String
Dim lngRecsAffected As Long
Try
If (Not (oDS) Is Nothing) Then
'open connection to Database
m_oCn.Open()
'attempt to update the data source
da.Update(oDS, strClassName)
End If
Catch updateException As System.Exception
Throw updateException
Finally
'Close the Database Connection
m_oCn.Close()
End Try
End Function
Dim cmdSel As OleDbCommand
Dim cmdIns As OleDbCommand
Dim cmdUpd As OleDbCommand
Dim cmdDel As OleDbCommand
Dim strSQL As String
'Setup Select Command
strSQL = "QryQuestion"
cmdSel = New OleDbCommand(strSQL, m_oCn)
cmdSel.CommandType = CommandType.StoredProcedure
'Setup Insert Command
strSQL = "addqryQuestion"
cmdIns = New OleDbCommand(strSQL, m_oCn)
cmdIns.CommandType = CommandType.StoredProcedure
cmdIns.Parameters.Add("QuestionText", OleDbType.LongVarWChar,
255, "Question_Text")
cmdIns.Parameters.Add("CorrectAnswer", OleDbType.BigInt, 1,
"Correct_Answer")
cmdIns.Parameters.Add("QuestionNumber", OleDbType.BigInt, 1,
"Question_Number")
'Setup Update Command
strSQL = "updqryQuestion"
cmdUpd = New OleDbCommand(strSQL, m_oCn)
cmdUpd.CommandType = CommandType.StoredProcedure
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionNumber",
OleDbType.BigInt, 50, "Question_Number"))
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionText",
OleDbType.LongVarWChar, 255, "Question_Text"))
cmdUpd.Parameters.Add(New OleDbParameter("@CorrectAnswer",
OleDbType.BigInt, 50, "Correct_Answer"))
cmdUpd.Parameters.Add(New OleDbParameter("@QuestionID",
OleDbType.BigInt, 50, "Question_ID")).SourceVersion =
DataRowVersion.Original
'Setup Delete Command
strSQL = "delqryQuestion"
cmdDel = New OleDbCommand(strSQL, m_oCn)
cmdDel.CommandType = CommandType.StoredProcedure
cmdDel.Parameters.Add("QuestionID", OleDbType.BigInt, 50,
"Question_ID")
initalizeDataAdapter(cmdSel, cmdIns, cmdUpd, cmdDel)
End Sub
Protected Sub initalizeDataAdapter(ByVal cmdSel As OleDbCommand, ByVal
cmdIns As OleDbCommand, ByVal cmdUpd As OleDbCommand, ByVal cmdDel As
OleDbCommand)
'Create and Setup Data Adapter
da = New OleDbDataAdapter
'Setup Select Command
da.SelectCommand = cmdSel
'Setup Insert Command
da.InsertCommand = cmdIns
'Setup Update Command
da.UpdateCommand = cmdUpd
'Setup Delete Command
da.DeleteCommand = cmdDel
'Destroy connection object
' m_oCn.Close()
End Sub