在循环中更新数据库 [英] Updating database in a loop
问题描述
我正在使用VB.NET 2003,SQL 2000和SqlDataAdapter。
对于tblA中colB =''abc''的每条记录,我想更新
中的值
colA。
在VB6中,使用ADO我可以通过记录集循环,设置colA的值和
调用Update方法。
如何在VB.NET和SqlDataAdapter中执行此操作?谢谢。
m_cmdSQL =新的SqlClient.SqlCommand
使用m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
结束
m_daSQL =新的SqlClient.SqlDataAdapter
m_dsSQL =新数据集
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
更新内存中的值,但不更新数据库
lRow = lRow + 1
下一页
您需要使用自己的更新逻辑创建/配置dataadapter的InsertCommand及其
CommandText属性。一旦你完成了这个,你只需添加:
m_daSQL.Update
代码。
" fniles" < fn **** @ pfmail.com在留言中写道
新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...
>我正在使用VB.NET 2003,SQL 2000和SqlDataAdapter。
对于tblA中的每条记录,其中colB ='' abc'',我想更新
colA中的值。
在VB6中,使用ADO我可以通过记录集循环,设置colA和
调用Update方法。
如何在VB.NET和SqlDataAdapter中执行此操作?谢谢。
m_cmdSQL =新的SqlClient.SqlCommand
使用m_cmdSQL
.Connection = adoCon
.CommandText = sSQL
结束
m_daSQL =新的SqlClient.SqlDataAdapter
m_dsSQL =新数据集
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
更新内存中的值,但不更新数据库
lRow = lRow + 1
下一页
谢谢。
对不起,我还是很困惑。
在我的例子中,我使用CommandText填充数据集select * from tblA
where [colB] =''abc''",然后我更新它。
如果我需要使用InsertCommand,我可以使用相同的DataAdapter / DataSet?
谢谢
m_cmdSQL =新的SqlClient.SqlCommand
使用m_cmdSQL
。 Connection = adoCon
.CommandText = sSQL
结束
m_daSQL =新SqlClient.SqlDataAdapter
m_dsSQL =新DataSet
m_daSQL.SelectCommand =" select * from tblA where [colB] =''abc''"
m_daSQL.Fill(m_dsSQL)
lRow = 0
每个aRow在m_dsSQL.Tables(0).Rows()
sSQL =" update tblA set"
sSQL = sSQL& (colA ="& lrow
sSQL = sSQL& (where [colB] =''abc'')
m_daSQL.Update ---如何使用已经填充的相同m_daSQL
with" ;从tblA中选择*,其中[colB] =''abc''" ?
lRow = lRow + 1
下一页
" Scott M." < s - *** @ nospam.nospamwrote in message
news:%2 ****************** @ TK2MSFTNGP03.phx.gbl。 ..
您需要使用自己的更新逻辑创建/配置dataadapter的InsertCommand及其
CommandText属性。一旦你完成了
,你只需添加:
m_daSQL.Update
代码。
" fniles" < fn **** @ pfmail.com在留言中写道
新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...
>>我正在使用VB.NET 2003,SQL 2000和SqlDataAdapter。
对于tblA中的每条记录,其中colB =''abc' ',我想在colA中更新值
。
在VB6中,使用ADO我可以通过记录集循环,设置colA的值
并调用Update方法。
我怎么能在VB.NET和SqlDataAdapter中做到这一点?谢谢。
m_cmdSQL =新的SqlClient.SqlCommand
使用m_cmdSQL
。Connect = adoCon
.CommandText = sSQL
结束
m_daSQL =新的SqlClient.SqlDataAdapter
m_dsSQL =新的数据集
m_daSQL.SelectCommand =" select * from tblA where [colB] =''abc''"
m_daSQL.Fill(m_dsSQL)
lRow = 0
对于每个aRow在m_dsSQL.Tables(0).Rows()
m_dsSQL.Tables(0).Rows(lRow).Item(" colA")= lrow - -this
更新内存中的值,但不在数据库中更新
lRow = lRow + 1
下一页
" fniles" < fn **** @ pfmail.com写在
新闻:#Z ************** @ TK2MSFTNGP03.phx.gbl:
< blockquote class =post_quotes>
对于tblA中colB =''abc''的每条记录,我想更新colA中的
值。
为什么不写一个SQL语句:
更新tblA set colA =''SomeValue''where colB =''abc''
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
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
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
"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''
这篇关于在循环中更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!