ADO.NET使用一个SQLCommandBuilder更新两个表 [英] ADO.NET Update two tables using one SQLCommandBuilder

查看:66
本文介绍了ADO.NET使用一个SQLCommandBuilder更新两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用一个数据适配器和一个命令构建器来更新amny表吗?

目前在我的数据适配器中,我查询两个表并将它们填充到两个表中

in一个数据集。当我对第二个表中的记录进行更改并调用

数据适配器的更新方法时,命令构建器更新命令

text用于第一个表。命令构建器是否可以处理两个表?


代码示例:

Dim oCOnn As New SqlConnection(Data Source = .; & _

" Initial

Catalog = Databasename;"& _

" Integrated Security = SSPI")

Dim oAdt As New SqlDataAdapter(Select * from location; select *

from vendor,oCOnn)

Dim oSet As New DataSet

Dim ocommbuild作为新的SqlCommandBuilder(oAdt)


oAdt.Fill(oSet)

oSet.Tables(1).Rows(1) .Item(" Notes")=" Hello"

Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet。表(1))

Can I use one Data Adapter and one Command Builder to update amny tables?
Currently in my data adapter I query two tables and fill them into two tables
in a data set. When I make a change to a record in the second table and call
the update method of the data adapter the command builders update command
text is for the first table. Can the command builder handle two tables?

Code example:

Dim oCOnn As New SqlConnection("Data Source=.;" & _
"Initial
Catalog=Databasename;" & _
"Integrated Security=SSPI")
Dim oAdt As New SqlDataAdapter("Select * from location; select *
from vendor", oCOnn)
Dim oSet As New DataSet
Dim ocommbuild As New SqlCommandBuilder(oAdt)

oAdt.Fill(oSet)
oSet.Tables(1).Rows(1).Item("Notes") = "Hello"
Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet.Tables(1))

推荐答案

只需使用两个单独的命令构建器,每个查询一个。或者,你可以

为它指定一个不同的选择查询,然后调用update-但是它更容易使用其中两个。此外,如果它们以任何形式相关,您可能希望在这些表上使用

DataRelation,只是为了确保客户端不会飞的任何事情都没有发生。当它回到

服务器时。


总而言之 - 我不是CommandBuilders的忠实粉丝 - 至少不是在

这个版本的框架 - 比尔·沃恩有一篇很好的文章说明为什么在
www.betav.com - >文章 - > MSDN - 来自

CommandBuilder的断奶开发人员。


暂时不说......这不是一个众所周知的风险,不管你信不信,

连接字符串也容易受到注入攻击,并且因为它允许用户数据创建连接字符串可能会让你进入

麻烦可能。因此,我会小心(即不要这样做

- ))。将其存储在配置文件或隔离存储中,理想情况下加密

。是的,你正在使用一个可信赖的连接,这样可以降低以纯文本形式存储它的风险 - 但这是一个很好的做法,如果你曾经的话,那就是$ b $如果要更改身份验证模型,代码就已经到位了。

请注意,我绝不会尝试演讲或其他任何事情 - 只是想想

我'如果你感兴趣的话,请提一下 - 当我得到一个带连接线的

注射攻击时 - 我很惊讶b / c我没有实现
这样的事情是可能的。


HTH,


比尔

" Hank1234" <哈****** @ discussions.microsoft.com>在消息中写道

news:45 ********************************** @ microsof t.com ...
Just use two separate commandbuilders, one for each query. Or, you can
specify a different select query for it and then call update- but it''s
easier to just use two of them. Also, you probably want to use a
DataRelation on those tables if they are related in any form, just to ensure
that nothing happens client side that won''t fly when it gets back to the
server.

All in all though - I''m not a big fan of CommandBuilders - at least not in
this version of the framework - Bill Vaughn has a great article on why at
www.betav.com ->Articles -> MSDN - Weaning Developers from the
CommandBuilder.

As an aside... It''s not a commonly known risk but believe it or not,
connection strings are susceptible to injection attacks as well and as it
stands, allowing user data to create the connection string could get you in
trouble potentially. As such, I''d be careful about that (ie don''t do it
-) ). Store it in a config file or in isolated storage and ideally encrypt
it. Yes, you''re using a trusted connection so that mitigates the risk of
having it stored in plain text - but it''s a good practice and if you ever n
eed to change the authentication model, the code will be in place already.
Please note that in no way am I trying to lecture or anything - just figured
I''d mention it in case you were interested - when I learned about an
injection attack with a connectionstring- I was quite surprised b/c I didn''t
realize such a thing was possible.

HTH,

Bill
"Hank1234" <Ha******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
我可以使用一个数据适配器和一个命令生成器来更新amny表吗?
目前在我的数据适配器中,我查询两个表并将它们填入两个表中>数据集中的表格
。当我对第二个表中的记录进行更改并且
调用数据适配器的更新方法时,命令构建器更新命令
文本用于第一个表。命令构建器可以处理两个表吗?

代码示例:

Dim oCOnn As New SqlConnection(" Data Source = .;"& _
" ; Initial
Catalog = Databasename;"& _
" Integrated Security = SSPI")
Dim oAdt As New SqlDataAdapter(" Select * from location; select *
来自供应商",oCOnn)
Dim oSet as New DataSet
Dim ocommbuild As New SqlCommandBuilder(oAdt)
oSet.Tables(1 ).Rows(1).Item(" Notes")=" Hello"
Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet.Tables( 1))
Can I use one Data Adapter and one Command Builder to update amny tables?
Currently in my data adapter I query two tables and fill them into two
tables
in a data set. When I make a change to a record in the second table and
call
the update method of the data adapter the command builders update command
text is for the first table. Can the command builder handle two tables?

Code example:

Dim oCOnn As New SqlConnection("Data Source=.;" & _
"Initial
Catalog=Databasename;" & _
"Integrated Security=SSPI")
Dim oAdt As New SqlDataAdapter("Select * from location; select *
from vendor", oCOnn)
Dim oSet As New DataSet
Dim ocommbuild As New SqlCommandBuilder(oAdt)

oAdt.Fill(oSet)
oSet.Tables(1).Rows(1).Item("Notes") = "Hello"
Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet.Tables(1))



我不知道如何使用两个不同的命令构建器

从一个数据适配器中的两个不同表中选择。当我创建一个

命令构建器时,它只接受数据适配器,因为我的数据适配器

在命令文本中有两个select语句如何创建第二个

构建器使用第二个select语句。我希望这是有道理的


" W.G。 Ryan MVP写道:
I don''t understand how to use two different command builders when I have
selected from two different tables in the one data adapter. When I create a
command builder it only accepts the data adapter and since my data adapter
had two select statements in the command text how do I create a second
builder to use the second select statement. I hope this makes sense

"W.G. Ryan MVP" wrote:
只需使用两个单独的命令构建器,每个查询一个。或者,你可以为它指定一个不同的选择查询,然后调用update-但是它更容易使用其中两个。此外,如果它们以任何形式相关,您可能希望在这些表上使用
DataRelation,只是为了确保客户端在返回到 www.betav.com - >文章 - > MSDN - 来自CommandBuilder的断奶开发人员。

暂且不说......这不是一个众所周知的风险,但不管你信不信,
连接字符串容易受到影响注入攻击以及它的立场,允许用户数据创建连接字符串可能会让你陷入潜在的麻烦。因此,我会小心(即不要这样做
- ))。将其存储在配置文件或隔离存储中,并理想地加密它。是的,您正在使用可信连接,以降低以纯文本格式存储的风险 - 但这是一种很好的做法,如果您曾经想要更改身份验证模型,代码已经到位。
请注意,我绝不会尝试演讲或任何事情 - 只是想想
我会在你感兴趣的时候提到它 - 当我得知一个<带有连接线的注射攻击 - 我很惊讶b / c我没有意识到这样的事情是可能的。

HTH,

比尔
Hank1234 <哈****** @ discussions.microsoft.com>在消息中写道
新闻:45 ********************************** @ microsof t.com。 ..
Just use two separate commandbuilders, one for each query. Or, you can
specify a different select query for it and then call update- but it''s
easier to just use two of them. Also, you probably want to use a
DataRelation on those tables if they are related in any form, just to ensure
that nothing happens client side that won''t fly when it gets back to the
server.

All in all though - I''m not a big fan of CommandBuilders - at least not in
this version of the framework - Bill Vaughn has a great article on why at
www.betav.com ->Articles -> MSDN - Weaning Developers from the
CommandBuilder.

As an aside... It''s not a commonly known risk but believe it or not,
connection strings are susceptible to injection attacks as well and as it
stands, allowing user data to create the connection string could get you in
trouble potentially. As such, I''d be careful about that (ie don''t do it
-) ). Store it in a config file or in isolated storage and ideally encrypt
it. Yes, you''re using a trusted connection so that mitigates the risk of
having it stored in plain text - but it''s a good practice and if you ever n
eed to change the authentication model, the code will be in place already.
Please note that in no way am I trying to lecture or anything - just figured
I''d mention it in case you were interested - when I learned about an
injection attack with a connectionstring- I was quite surprised b/c I didn''t
realize such a thing was possible.

HTH,

Bill
"Hank1234" <Ha******@discussions.microsoft.com> wrote in message
news:45**********************************@microsof t.com...
我可以使用一个数据适配器和一个命令构建器来更新amny表吗?
目前在我的数据适配器中,我查询两个表并将它们填入两个表中。 />在数据集中。当我对第二个表中的记录进行更改并且
调用数据适配器的更新方法时,命令构建器更新命令
文本用于第一个表。命令构建器可以处理两个表吗?

代码示例:

Dim oCOnn As New SqlConnection(" Data Source = .;"& _
" ; Initial
Catalog = Databasename;"& _
" Integrated Security = SSPI")
Dim oAdt As New SqlDataAdapter(" Select * from location; select *
来自供应商",oCOnn)
Dim oSet as New DataSet
Dim ocommbuild As New SqlCommandBuilder(oAdt)
oSet.Tables(1 ).Rows(1).Item(" Notes")=" Hello"
Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet.Tables( 1))
Can I use one Data Adapter and one Command Builder to update amny tables?
Currently in my data adapter I query two tables and fill them into two
tables
in a data set. When I make a change to a record in the second table and
call
the update method of the data adapter the command builders update command
text is for the first table. Can the command builder handle two tables?

Code example:

Dim oCOnn As New SqlConnection("Data Source=.;" & _
"Initial
Catalog=Databasename;" & _
"Integrated Security=SSPI")
Dim oAdt As New SqlDataAdapter("Select * from location; select *
from vendor", oCOnn)
Dim oSet As New DataSet
Dim ocommbuild As New SqlCommandBuilder(oAdt)

oAdt.Fill(oSet)
oSet.Tables(1).Rows(1).Item("Notes") = "Hello"
Debug.WriteLine(ocommbuild.GetUpdateCommand.Comman dText)

oAdt.Update(oSet.Tables(1))




Hank,


Bill写道在我看来并不重要。

但是当你想要结果的时候像这样的dbuilder。

Dim mycmd作为新的SQLCommandBuilder(DA1)

mycmd = new SQLCommandBuilder(DA2)


给它吧给AFAIK带来的优势并不多于这个

Dim myCmd1作为新的SQLCommandBuilder(DA1)

Dim myCmd2作为新的SQLCommandBuilder(DA2)


我更喜欢最后一个。


只是我的想法


Cor
Hank,

Bill wrote that in as in my opinion does not matter.
However when you want the result of the commandbuilder like this.
Dim mycmd as new SQLCommandBuilder(DA1)
mycmd = new SQLCommandBuilder(DA2)

Does give it give AFAIK not much advantages above this
Dim myCmd1 as new SQLCommandBuilder(DA1)
Dim myCmd2 as new SQLCommandBuilder(DA2)

I prefer the last.

Just my thought

Cor


这篇关于ADO.NET使用一个SQLCommandBuilder更新两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆