473,473 Members | 4,204 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Updating database in a loop

I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value in
colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA and
call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next
Dec 14 '06 #1
16 6277
You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that you
simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value in
colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA and
call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next


Dec 14 '06 #2
Thank you.
I am sorry, I am still confused.
In my example, I Fill the dataset using CommandText "select * from tblA
where [colB] = 'abc'", before I update it.
If I need to use InsertCommand, can I use the same DataAdapter/DataSet ?
Thanks

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
sSQL = "update tblA set"
sSQL = sSQL & (" colA = " & lrow)
sSQL = sSQL & (" where [colB] = 'abc')
m_daSQL.Update ---how can I use the same m_daSQL that is already filled
with the "select * from tblA where [colB] = 'abc'" ?
lRow = lRow + 1
Next

"Scott M." <s-***@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that
you simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value
in colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next



Dec 14 '06 #3
"fniles" <fn****@pfmail.comwrote in
news:#Z**************@TK2MSFTNGP03.phx.gbl:
For every record in tblA where colB = 'abc', I want to update the
value in colA.
Why don't you write a SQL statement:

Update tblA set colA = 'SomeValue' where colB = 'abc'
Dec 14 '06 #4
Your DataAdapter goes and gets a copy of the data you want (based on the
Select statement you set up in the DataAdapter's select command). The copy
is placed in your dataset. You make changes to your dataset and then you
call the DataAdapter's update method. But you must set up the UpdateCommand
so that it does the update you want it to.

By the way, the m_ prefix you are using is not a recommened naming
convention and will cause more confusion than anything else. Also, don't
use the prefix of "ado" for your connection name since you aren't using ado
anyway, you're using ADO.NET.

Here's your code again (modified for better naming conventions and correct
coding):

Diim con As New SqlClient.SqlClientConnection("connection string here")
Dim da As New SqlClient.SqlDataAdapter("select * from tblA where [colB] =
'abc'", con)
Dim updateCommand As New SqlClient.SqlClientCommand("UPDATE tblA blah, blah,
blah")
Dim ds As New DataSet

da.UpdateCommand = updateCommand

Try
daSQL.Fill(ds, "tblA")
Dim i As Integer
For i = 0 To ds.Tables(0).Rows().Count -1
'Forget about update statements here, you are working with
'a disconnected DataSet now, so just make whatever changes
'you need to the DataRows that this loop iterates over
If ds.Tables(0).Rows(i).Columns("colB") = "abc" Then
ds.Tables(0).Rows(i).Columns("colA") = i
End If
Next

da.Update()

Catch ex As Exception
'handle exceptions here
Finally
con.close() 'not needed if the connection was closed to begin with
End Try



"fniles" <fn****@pfmail.comwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
Thank you.
I am sorry, I am still confused.
In my example, I Fill the dataset using CommandText "select * from tblA
where [colB] = 'abc'", before I update it.
If I need to use InsertCommand, can I use the same DataAdapter/DataSet ?
Thanks

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
sSQL = "update tblA set"
sSQL = sSQL & (" colA = " & lrow)
sSQL = sSQL & (" where [colB] = 'abc')
m_daSQL.Update ---how can I use the same m_daSQL that is already filled
with the "select * from tblA where [colB] = 'abc'" ?
lRow = lRow + 1
Next

"Scott M." <s-***@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that
you simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value
in colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next




Dec 14 '06 #5
lose the training wheels; learn how to WRITE SQL and don't rely on
recordsets to update data

fucking retard

this is a simple command; if you can't write subqueries and shit then
you should STFU and go work at mcDonalds

-Aaron
fniles wrote:
I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value in
colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA and
call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next
Dec 14 '06 #6
fniles wrote:
<back posted/>

For the specific update you want to perform (setting each colA to a
distinct, zero-based index for all rows where colB = 'abc') , you don't
even need to fetch the records from the DB; the following would
suffice:

<aircode>
Dim SQL As String = "declare @val int; " _
& "set @val = -1; " _
& "update tblA Set @val = colA = @val + 1 where colB = 'abc'"

'Assuming a connection Con exists and is already open
Dim Cmd As New SqlClient.SqlCommand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQuery()
</aircode>

Now, if you want to use a more generic approach, then you must set up
an update command, as pointed out by Scott.

The update command is a SQL string that would update a specific row
given a set of new values, passed as parameters. For instance, it could
be:

"Update tblA set colA = @colA where tblAID = @tblAID"

As you can see, you'd need a way to inequivocally specify the row you
need to update, probably using a primary key (named tblAID, here).

I'm not sure if you can use a different set of columns than the ones
used by the select query, I guess you'll have to do some testings.

Just to give you a headstart in the matter, the following would perform
the update using this approach:

<aircode>
'Assuming an open connection Con

Dim Adapter As New SqlClient.SqlDataAdapter

'the SELECT cmd
Dim Cmd As New SqlClient.SqlCommand( _
"select tblAID, colA from tblA where colB = 'abc'", _
Con)
Adapter.SelectCommand = Cmd

'the UPDATE cmd
Cmd = New SqlClient.SqlCommand( _
"update tblA set colA=@colA where tblAID=@tblAID", _
Con)
Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID")
Adapter.UpdateCommand = Cmd

'retrieves the data
Dim Ds As New DataSet
Adapter.Fill(Ds)

'modify rows
Dim Index As Integer = 0
For Each Row As DataRow In Ds.Tables(0).Rows
Row("colA") = Index
Index += 1
Next

'update the source table
Adapter.Update(Ds)
</aircode>

HTH.

Regards,

Branco.

I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value in
colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA and
call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next
Dec 14 '06 #7
Fniles,

In addition to Scott,

You can in simple situations like this as well use the commandbuilder, that
makes dynamicly the Insert, the Update and Delete commands for you.

dim cmd as New SqlClient.Commandbuilder(m_daSQL)
(_daSQL.Update(m_dsSQL)
(Like Scott I don't see the use for the m_. This has only sense to distinct
a variable private member where that would have the same name as a property)

I hope this helps,

Cor

"fniles" <fn****@pfmail.comschreef
in bericht news:uH**************@TK2MSFTNGP03.phx.gbl...
Thank you.
I am sorry, I am still confused.
In my example, I Fill the dataset using CommandText "select * from tblA
where [colB] = 'abc'", before I update it.
If I need to use InsertCommand, can I use the same DataAdapter/DataSet ?
Thanks

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
sSQL = "update tblA set"
sSQL = sSQL & (" colA = " & lrow)
sSQL = sSQL & (" where [colB] = 'abc')
m_daSQL.Update ---how can I use the same m_daSQL that is already filled
with the "select * from tblA where [colB] = 'abc'" ?
lRow = lRow + 1
Next

"Scott M." <s-***@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that
you simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value
in colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next




Dec 14 '06 #8
Thank you.
I have a few questions on your method:
Dim updateCommand As New SqlClient.SqlClientCommand("UPDATE tblA blah,
blah, blah")
Is the blah blah blah something like
"UPDATE tblA set ColA = " & lROW ?
If that is the case, at the time I assigned it (before the For loop), I do
not know the lRow yet.
>da.Update()
This requires a parameter, so should I set the parameter to ds (the DataSet)
?

Thanks.

"Scott M." <s-***@nospam.nospamwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...
Your DataAdapter goes and gets a copy of the data you want (based on the
Select statement you set up in the DataAdapter's select command). The
copy is placed in your dataset. You make changes to your dataset and then
you call the DataAdapter's update method. But you must set up the
UpdateCommand so that it does the update you want it to.

By the way, the m_ prefix you are using is not a recommened naming
convention and will cause more confusion than anything else. Also, don't
use the prefix of "ado" for your connection name since you aren't using
ado anyway, you're using ADO.NET.

Here's your code again (modified for better naming conventions and correct
coding):

Diim con As New SqlClient.SqlClientConnection("connection string here")
Dim da As New SqlClient.SqlDataAdapter("select * from tblA where [colB] =
'abc'", con)
Dim updateCommand As New SqlClient.SqlClientCommand("UPDATE tblA blah,
blah, blah")
Dim ds As New DataSet

da.UpdateCommand = updateCommand

Try
daSQL.Fill(ds, "tblA")
Dim i As Integer
For i = 0 To ds.Tables(0).Rows().Count -1
'Forget about update statements here, you are working with
'a disconnected DataSet now, so just make whatever changes
'you need to the DataRows that this loop iterates over
If ds.Tables(0).Rows(i).Columns("colB") = "abc" Then
ds.Tables(0).Rows(i).Columns("colA") = i
End If
Next

da.Update()

Catch ex As Exception
'handle exceptions here
Finally
con.close() 'not needed if the connection was closed to begin with
End Try



"fniles" <fn****@pfmail.comwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
>Thank you.
I am sorry, I am still confused.
In my example, I Fill the dataset using CommandText "select * from tblA
where [colB] = 'abc'", before I update it.
If I need to use InsertCommand, can I use the same DataAdapter/DataSet ?
Thanks

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
sSQL = "update tblA set"
sSQL = sSQL & (" colA = " & lrow)
sSQL = sSQL & (" where [colB] = 'abc')
m_daSQL.Update ---how can I use the same m_daSQL that is already filled
with the "select * from tblA where [colB] = 'abc'" ?
lRow = lRow + 1
Next

"Scott M." <s-***@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl. ..
>>You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that
you simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl.. .
I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value
in colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next




Dec 14 '06 #9
Thank you.
In your more generic approach, is this the correct way to set the value for
tblAID ?
Cmd = New SqlClient.SqlCommand( "update tblA set colA=@colA where
tblAID=@tblAID", Con)
Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID").VALUE = MyID
Adapter.UpdateCommand = Cmd

m_UpdateCmdSQL.Parameters.Add("@" & sParameterName, SqlDbType.VarChar,
sParameterSize).Value = sParameter

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
fniles wrote:
<back posted/>

For the specific update you want to perform (setting each colA to a
distinct, zero-based index for all rows where colB = 'abc') , you don't
even need to fetch the records from the DB; the following would
suffice:

<aircode>
Dim SQL As String = "declare @val int; " _
& "set @val = -1; " _
& "update tblA Set @val = colA = @val + 1 where colB = 'abc'"

'Assuming a connection Con exists and is already open
Dim Cmd As New SqlClient.SqlCommand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQuery()
</aircode>

Now, if you want to use a more generic approach, then you must set up
an update command, as pointed out by Scott.

The update command is a SQL string that would update a specific row
given a set of new values, passed as parameters. For instance, it could
be:

"Update tblA set colA = @colA where tblAID = @tblAID"

As you can see, you'd need a way to inequivocally specify the row you
need to update, probably using a primary key (named tblAID, here).

I'm not sure if you can use a different set of columns than the ones
used by the select query, I guess you'll have to do some testings.

Just to give you a headstart in the matter, the following would perform
the update using this approach:

<aircode>
'Assuming an open connection Con

Dim Adapter As New SqlClient.SqlDataAdapter

'the SELECT cmd
Dim Cmd As New SqlClient.SqlCommand( _
"select tblAID, colA from tblA where colB = 'abc'", _
Con)
Adapter.SelectCommand = Cmd

'the UPDATE cmd
Cmd = New SqlClient.SqlCommand( _
"update tblA set colA=@colA where tblAID=@tblAID", _
Con)
Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID")
Adapter.UpdateCommand = Cmd

'retrieves the data
Dim Ds As New DataSet
Adapter.Fill(Ds)

'modify rows
Dim Index As Integer = 0
For Each Row As DataRow In Ds.Tables(0).Rows
Row("colA") = Index
Index += 1
Next

'update the source table
Adapter.Update(Ds)
</aircode>

HTH.

Regards,

Branco.

>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the value
in
colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and
call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next

Dec 14 '06 #10
fniles wrote:
Thank you.
In your more generic approach, is this the correct way to set the value for
tblAID ?
Cmd = New SqlClient.SqlCommand( "update tblA set colA=@colA where
tblAID=@tblAID", Con)
Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID").VALUE = MyID
Adapter.UpdateCommand = Cmd

m_UpdateCmdSQL.Parameters.Add("@" & sParameterName, SqlDbType.VarChar,
sParameterSize).Value = sParameter
Nope. You don't need to set the parameters values, the DataAdapter will
do that for you for each modified row when you call the Update()
method.

I'm not sure if I understand you correctly. It seems you want to update
also the field used as ID. If this is the case, then you must indicate
so in the update string. Notice, however, that then you must request
the filter (the "where" part) to use *the original ID value* when
looking for the row:

<aircode>
'Specify the original field value as key
Cmd = New SqlClient.SqlCommand( _
"update tblA set colA=@colA, tblAID=@tblAID " _
& "where tblAID=@OriginalID", _
Con)

Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID")

'uses the original value for the field
Cmd.Parameters.Add( _
"@OriginalID", SqlDbType.Int, 5, "tblAID" _
).SourceVersion = DataRowVersion.Original
</aircode>

HTH.

Regards,

Branco.

Dec 14 '06 #11
Thank you.
Actually, this is what I am trying to do:
Say I have a table Position like the following:
Account MasterAccount Cash Profit
123 999 1000 10
345 999 2000 20
999
I am looping thru this table looking for MasterAccount that is not blank (in
this case Account 123 and 345). For each of those records, I want to update
cash for MasterAccount with cash from this Account. In the example, I want
to update cash in Account 999 with cash from Account 123 and Account 345.
The result will be for Account 999, cash = 3000.
Because of this, I need to supply the Parameter value for MasterAccount and
Account inside the loop. Is this correct or I don't have to set this
Parameter values ?
Also, is it correct in that my case I want to call the Update method inside
the loop ?
Thank you.

sSQL = "update Position set cash = @Cash where masteraccount =
@MasterAccount and Account = @Account"
cmdSQL = New SqlClient.SqlCommand
With cmdSQL
.Connection = adoCon
.CommandText = "select * from position where [masteraccount] <''"
End With
daSQL = New SqlClient.SqlDataAdapter
dsSQL = New DataSet
daSQL.SelectCommand = cmdSQL
daSQL.Fill(dsSQL)
'Update Command
UpdateCmdSQL = New SqlClient.SqlCommand
UpdateCmdSQL.CommandText = sUpdateSQL
UpdateCmdSQL.Connection = adoCon
daSQL.UpdateCommand = UpdateCmdSQL
UpdateCmdSQL.Parameters.Add("@Cash", SqlDbType.Money, 8, "Cash")
UpdateCmdSQL.Parameters.Add("@MasterAccount", SqlDbType.VarChar, 50,
"MasterAccount")
UpdateCmdSQL.Parameters.Add("@Account", SqlDbType.VarChar, 10, "Account")
lRow = 0

For Each aRow In rsMaster.Rows
MasterAccount = dsSQL.Tables(0).Rows(lRow).Item("MasterAccount")
Account = dsSQL.Tables(0).Rows(lRow).Item("account")
'Read Cash and profit from MasterAccount
cmdSQLRead = New SqlClient.SqlCommand
With cmdSQLRead
.Connection = adoCon
.CommandText = "select * from position where ACCOUNT = '" &
MasterAccount & "'"
End With
drSQL = cmdSQLRead.ExecuteReader()
Do While drSQL.Read
dsSQL.Tables(0).Rows(lRow).Item("Cash") =
dsSQL.Tables(0).Rows(lRow).Item("Cash") + drSQL.Item("cash")
Loop
'----------------------------DO I need the next 2 LINE OF CODES HERE
? ------------------------------------
UpdateCmdSQL.Parameters("@MasterAccount").Value = MasterAccount
UpdateCmdSQL.Parameters("@Account").Value = Account
'-------------------------------------------------------------------------------------------------------------------
daSQL.Update(dsSQL) '-----IS THIS CORRECT TO CALL the UPDATE method
INSIDE the FOR LOOP ?
lRow = lRow + 1
Next

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
fniles wrote:
>Thank you.
In your more generic approach, is this the correct way to set the value
for
tblAID ?
Cmd = New SqlClient.SqlCommand( "update tblA set colA=@colA where
tblAID=@tblAID", Con)
Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID").VALUE = MyID
Adapter.UpdateCommand = Cmd

m_UpdateCmdSQL.Parameters.Add("@" & sParameterName, SqlDbType.VarChar,
sParameterSize).Value = sParameter

Nope. You don't need to set the parameters values, the DataAdapter will
do that for you for each modified row when you call the Update()
method.

I'm not sure if I understand you correctly. It seems you want to update
also the field used as ID. If this is the case, then you must indicate
so in the update string. Notice, however, that then you must request
the filter (the "where" part) to use *the original ID value* when
looking for the row:

<aircode>
'Specify the original field value as key
Cmd = New SqlClient.SqlCommand( _
"update tblA set colA=@colA, tblAID=@tblAID " _
& "where tblAID=@OriginalID", _
Con)

Cmd.Parameters.Add("@colA", SqlDbType.Int, 5, "colA")
Cmd.Parameters.Add("@tblAID", SqlDbType.Int, 5, "tblAID")

'uses the original value for the field
Cmd.Parameters.Add( _
"@OriginalID", SqlDbType.Int, 5, "tblAID" _
).SourceVersion = DataRowVersion.Original
</aircode>

HTH.

Regards,

Branco.

Dec 14 '06 #12
fniles wrote:
<snip>
Actually, this is what I am trying to do:
Say I have a table Position like the following:
Account MasterAccount Cash Profit
123 999 1000 10
345 999 2000 20
999
I am looping thru this table looking for MasterAccount that is not blank (in
this case Account 123 and 345). For each of those records, I want to update
cash for MasterAccount with cash from this Account. In the example, I want
to update cash in Account 999 with cash from Account 123 and Account 345.
The result will be for Account 999, cash = 3000.
<snip>

If this is the case, SQL is your friend (err... mostly): the following
commands will update the master accounts for you (no need looping):

<aircode>
Dim SQL As String = "update Position " _
& "set Cash = Cash + b.Total " _
& "from Position as a " _
& "join ( " _
& "select MasterAccount, Total = Sum(Cash) " _
& " from Position where MasterAccount is not null " _
& " group by MasterAccount) as b " _
& "on a.Account = b.MasterAccount"

'Assuming an open connection in Con
Dim Cmd As New SqlClient.SqlCommand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQuery()
</aircode>

As with the majority of the code posted in foruns, don't run this on
your production data =)

HTH.

Regards,

Branco.

Dec 14 '06 #13

Branco Medeiros wrote:
As with the majority of the code posted in foruns, don't run this on
your production data =)
I mean, not before you test it first, of course.

B.

Dec 14 '06 #14
No, the update statement wouldn't be referring to rows of the DataSet, it
would be referring to column names in the DataSet and the fields those
columns map to in your database.

The update method should be passed the dataset as its argument.
"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Thank you.
I have a few questions on your method:
>Dim updateCommand As New SqlClient.SqlClientCommand("UPDATE tblA blah,
blah, blah")
Is the blah blah blah something like
"UPDATE tblA set ColA = " & lROW ?
If that is the case, at the time I assigned it (before the For loop), I do
not know the lRow yet.
>>da.Update()
This requires a parameter, so should I set the parameter to ds (the
DataSet) ?

Thanks.

"Scott M." <s-***@nospam.nospamwrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...
>Your DataAdapter goes and gets a copy of the data you want (based on the
Select statement you set up in the DataAdapter's select command). The
copy is placed in your dataset. You make changes to your dataset and
then you call the DataAdapter's update method. But you must set up the
UpdateCommand so that it does the update you want it to.

By the way, the m_ prefix you are using is not a recommened naming
convention and will cause more confusion than anything else. Also, don't
use the prefix of "ado" for your connection name since you aren't using
ado anyway, you're using ADO.NET.

Here's your code again (modified for better naming conventions and
correct coding):

Diim con As New SqlClient.SqlClientConnection("connection string here")
Dim da As New SqlClient.SqlDataAdapter("select * from tblA where [colB]
= 'abc'", con)
Dim updateCommand As New SqlClient.SqlClientCommand("UPDATE tblA blah,
blah, blah")
Dim ds As New DataSet

da.UpdateCommand = updateCommand

Try
daSQL.Fill(ds, "tblA")
Dim i As Integer
For i = 0 To ds.Tables(0).Rows().Count -1
'Forget about update statements here, you are working with
'a disconnected DataSet now, so just make whatever changes
'you need to the DataRows that this loop iterates over
If ds.Tables(0).Rows(i).Columns("colB") = "abc" Then
ds.Tables(0).Rows(i).Columns("colA") = i
End If
Next

da.Update()

Catch ex As Exception
'handle exceptions here
Finally
con.close() 'not needed if the connection was closed to begin with
End Try



"fniles" <fn****@pfmail.comwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
>>Thank you.
I am sorry, I am still confused.
In my example, I Fill the dataset using CommandText "select * from tblA
where [colB] = 'abc'", before I update it.
If I need to use InsertCommand, can I use the same DataAdapter/DataSet ?
Thanks

m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
sSQL = "update tblA set"
sSQL = sSQL & (" colA = " & lrow)
sSQL = sSQL & (" where [colB] = 'abc')
m_daSQL.Update ---how can I use the same m_daSQL that is already
filled with the "select * from tblA where [colB] = 'abc'" ?
lRow = lRow + 1
Next

"Scott M." <s-***@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl.. .
You'll need to create/configure the dataadapter's InsertCommand and its
CommandText property with your own update logic. Once you've done that
you simply add:

m_daSQL.Update

to your code.

"fniles" <fn****@pfmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl. ..
>I am using VB.NET 2003, SQL 2000, and SqlDataAdapter.
For every record in tblA where colB = 'abc', I want to update the
value in colA.
In VB6, using ADO I can loop thru the recordset,set the values of colA
and call the Update method.
How can I do this in VB.NET and SqlDataAdapter ? Thank you.
>
m_cmdSQL = New SqlClient.SqlCommand
With m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
End With
m_daSQL = New SqlClient.SqlDataAdapter
m_dsSQL = New DataSet
m_daSQL.SelectCommand = "select * from tblA where [colB] = 'abc'"
m_daSQL.Fill(m_dsSQL)
lRow = 0
For Each aRow In m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item("colA") = lrow --this
updates the value in memory, but not in the database
lRow = lRow + 1
Next
>
>




Dec 14 '06 #15
Thank you again.
For future reference, if I want to do a similar thing from a program (not
SQL),
do I need the following 2 statements inside the FOR loop ?
UpdateCmdSQL.Parameters("@MasterAccount").Value = MasterAccount
UpdateCmdSQL.Parameters("@Account").Value = Account

Also, I should call the Update method from outside the loop, shouldn't I ?

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
fniles wrote:
<snip>
>Actually, this is what I am trying to do:
Say I have a table Position like the following:
Account MasterAccount Cash Profit
123 999 1000 10
345 999 2000 20
999
I am looping thru this table looking for MasterAccount that is not blank
(in
this case Account 123 and 345). For each of those records, I want to
update
cash for MasterAccount with cash from this Account. In the example, I
want
to update cash in Account 999 with cash from Account 123 and Account 345.
The result will be for Account 999, cash = 3000.
<snip>

If this is the case, SQL is your friend (err... mostly): the following
commands will update the master accounts for you (no need looping):

<aircode>
Dim SQL As String = "update Position " _
& "set Cash = Cash + b.Total " _
& "from Position as a " _
& "join ( " _
& "select MasterAccount, Total = Sum(Cash) " _
& " from Position where MasterAccount is not null " _
& " group by MasterAccount) as b " _
& "on a.Account = b.MasterAccount"

'Assuming an open connection in Con
Dim Cmd As New SqlClient.SqlCommand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQuery()
</aircode>

As with the majority of the code posted in foruns, don't run this on
your production data =)

HTH.

Regards,

Branco.

Dec 14 '06 #16
I think you have a fundamental misunderstanding of what's going on here.

You would only use a loop if you wanted to iterate over the rows in a
DataSet. But, as you are iterating over the rows of a DataSet, you aren't
concerned with update statements or command objects or parameters, you
simply make manual changes to the columns of the row in the dataset that
need new values.

The DataAdapter does the "actual" database updating for you, but it must
know how you want the update done, so you build an UpdateCommand and
configure the command (with or without parameters as needed), so that when
you call the Update() method of the DataAdapter, it knows how to take the
manual changes you made to your DataSet and update the actual data in the
database.


"fniles" <fn****@pfmail.comwrote in message
news:um**************@TK2MSFTNGP03.phx.gbl...
Thank you again.
For future reference, if I want to do a similar thing from a program (not
SQL),
do I need the following 2 statements inside the FOR loop ?
UpdateCmdSQL.Parameters("@MasterAccount").Value = MasterAccount
UpdateCmdSQL.Parameters("@Account").Value = Account

Also, I should call the Update method from outside the loop, shouldn't I ?

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
>fniles wrote:
<snip>
>>Actually, this is what I am trying to do:
Say I have a table Position like the following:
Account MasterAccount Cash Profit
123 999 1000 10
345 999 2000 20
999
I am looping thru this table looking for MasterAccount that is not blank
(in
this case Account 123 and 345). For each of those records, I want to
update
cash for MasterAccount with cash from this Account. In the example, I
want
to update cash in Account 999 with cash from Account 123 and Account
345.
The result will be for Account 999, cash = 3000.
<snip>

If this is the case, SQL is your friend (err... mostly): the following
commands will update the master accounts for you (no need looping):

<aircode>
Dim SQL As String = "update Position " _
& "set Cash = Cash + b.Total " _
& "from Position as a " _
& "join ( " _
& "select MasterAccount, Total = Sum(Cash) " _
& " from Position where MasterAccount is not null " _
& " group by MasterAccount) as b " _
& "on a.Account = b.MasterAccount"

'Assuming an open connection in Con
Dim Cmd As New SqlClient.SqlCommand(SQL, Con)
Dim Count As Integer = Cmd.ExecuteNonQuery()
</aircode>

As with the majority of the code posted in foruns, don't run this on
your production data =)

HTH.

Regards,

Branco.


Dec 15 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Johnson | last post by:
Here is my dilema. I have a 120 GB database that I need to mask customer credit card numbers in. The field is a varchar (16). I need to update the field so that we only store the first 4 numbers...
1
by: Darryl Neale | last post by:
I have been tasked with reviving an old database that stopped working about 3 years ago :( On one of the tabs is a list of links to other databases utilising the Hyperlink feature of MS Access...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
2
by: Aaron Reimann | last post by:
I have a lot of check boxes. This is an update of the check boxes, I want something was checked, then to do an insert (which is currently working), if something is no longer checked...delete the...
11
by: bbasberg | last post by:
Hello, I have been struggling with this problem for DAYS and have googled my heart out as well as reading any books I could get my hands on but I cannot find any specific references to my problem....
10
by: chimambo | last post by:
Hi All, I have a little problem. I am retrieving records from a table and I want to update the records using checkboxes. I am able to display the database record quite alright and I have created...
3
by: Spoogledrummer | last post by:
Hi it's me again, still working on the sam 5 minute problem so feeling kind of thick now. I've dumped the idea of using a textarea for now and am using a textbox instead but am struggling when it...
5
by: Bill Schanks | last post by:
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...
4
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.