Re: How do I delete all rows in a SQL Server table when my ADO DataSet has more than one table

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Brad,

Why are you trying to delete the row through the DataSet? You do not need to
load all the data to the client side to delete all the rows. Just use
ExecuteNoQuery method of the SqlCommand to execute DELETE SQL statements and
it will work much faster

--
Val Mazur
Microsoft MVP

http://xport.mvps.org



"Brad Marshall" <bradley.marshall@xxxxxxxxxxx> wrote in message
news:uo$mtHJbFHA.2900@xxxxxxxxxxxxxxxxxxxxxxx
>I want to delete all rows in a SQL Server table at the start of a
>procedure. I have a SqlDataAdapter with a DataSet that has two tables. I
>have the following code to delete all records in both tables. These tables
>are populated from the last execution of the procedure. I fail with a
>concurrency violation on the first delete, but the records are deleted from
>the SQL table! If I execute again after the exception terminated the
>procedure, I succeed on the first Update but fail again on the second
>update. Because I am updating more than one table in the dataset I can't
>use the command builder; rather, I must set a DeleteCommand for the
>adapter. What am I doing wrong?
>
> Dim myConnectionString As String = "Initial Catalog=myDataBase;Data
> Source=localhost;Integrated Security=SSPI;Server=myServer"
> Dim myConnection As New SqlConnection(myConnectionString)
> Dim OmsAdapter As New SqlDataAdapter
> Dim dsMyProject As DataSet
> Dim LogDeleteCommand As New SqlCommand("DELETE TransLog", myConnection)
> Dim ResultDeleteCommand As New SqlCommand("DELETE Result", myConnection)
> Dim ResultRow As DataRow
> Dim LogRow As DataRow
> Dim myQuery As String
> Dim myCommand As New SqlCommand()
> dsMyProject = New DataSet("myProject")
>
> ' Clear out the Result table
>
> myQuery = "SELECT * from Result"
> myCommand.CommandText = myQuery
> OmsAdapter.SelectCommand = myCommand
> OmsAdapter.Fill(dsMyProject, "Result")
>
> For Each ResultRow In dsMyProject.Tables("Result").Rows
> ResultRow.Delete()
> Next
>
> OmsAdapter.DeleteCommand = ResultDeleteCommand
> OmsAdapter.Update(dsMyProject, "Result")
>
> ' Clear out the OMS Results table
>
> myQuery = "SELECT * from TransLog"
> myCommand.CommandText = myQuery
> OmsAdapter.SelectCommand = myCommand
> OmsAdapter.Fill(dsMyProject, "TransLog")
>
> For Each LogRow In dsMyProject.Tables("TransLog").Rows
> LogRow.Delete()
> Next
>
> OmsAdapter.DeleteCommand = LogDeleteCommand
> OmsAdapter.Update(dsMyProject, "TransLog")
>


.



Relevant Pages

  • RE: How Do I Extract Data from my Form to load new table records?
    ... Steve: I have adapted your code as follows, but am ... Dim strSQL As String ... Open a "template table with a number of predefined "standard" rows ... file has 10 records in it, then every time I execute this, I would be adding ...
    (microsoft.public.access.formscoding)
  • Re: Execute Method for Find Object
    ... During each iteration of the While loop, if .Execute returns ... Dim strSearch As String, strTerm As String ... Dim intIndex As Integer, intCounter As Integer, intLast As Integer ...
    (microsoft.public.word.vba.beginners)
  • Re: Database Extract Automation
    ... > database to execute JET SQL statements to move the data across. ... > Dim dbWSec 'As DAO.Workspace ... > 'Add code here to make sure UnsecuredDB doesn't ...
    (microsoft.public.access.externaldata)
  • Re: Select several words
    ... .Execute findText:=oldPart, _ ... Word MVP web site http://word.mvps.org ... Dim ChangeDoc As Document, RefDoc As Document ...
    (microsoft.public.word.docmanagement)
  • Re: Looping!
    ... Well, McKirahan, this too takes about 30-35 seconds to execute! ... Now for the drop-downs - the no. ... the strSQL2 queries to populate the drop-downs. ... Dim strSQL1,strSQL2 ...
    (microsoft.public.inetserver.asp.db)