使用表适配器从数据源中删除行 [英] Deleting a Row from the Data Source with Table Adapter

查看:74
本文介绍了使用表适配器从数据源中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



尝试将表格行删除传递给数据源,但是我很失败。\\ b
崩溃。这是代码:


Private Sub btnDeleteIngr_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理btnDeleteIngr.Click

Dim vbResponse As Integer

vbResponse = MessageBox.Show("你确定要

删除" + tbIngr.Text +"? ;,确认删除,

MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)

如果vbResponse = 1那么

Dim dr As DataRow

Dim drv As DataRowView

drv = bsIngredients.Current

dr = drv.Row

dr.Delete()

bsIngredients.MoveNext()

taIngredients.Update(dsMenuPlanner.Ingredients)

dsMenuPlanner.AcceptChanges()

结束如果

End Sub


这是taIngredients.Update(dsMenuPlanner.Ingredient s)。

崩溃的一行。

传递带删除行的DataRow集合时,错误是更新需要有效的DeleteCommand。


我无法弄清楚如何解决这个。有人可以帮忙吗?


谢谢,

兰迪

解决方案

< blockquote>您的数据适配器必须具有为其定义的InsertCommand,DeleteCommand和UpdateCommand

对象才能执行更新。


Robin S.

--------------------------------

" Randy" < sp *********** @ gmail.comwrote in message

news:11 ******************* ***@p77g2000hsh.googlegr oups.com ...




尝试传递表行删除数据源,但是我好了b $ b崩溃了。这是代码:


Private Sub btnDeleteIngr_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理btnDeleteIngr.Click

Dim vbResponse As Integer

vbResponse = MessageBox.Show("你确定要

删除" + tbIngr.Text +"? ;,确认删除,

MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)

如果vbResponse = 1那么

Dim dr As DataRow

Dim drv As DataRowView

drv = bsIngredients.Current

dr = drv.Row

dr.Delete()

bsIngredients.MoveNext()

taIngredients.Update(dsMenuPlanner.Ingredients)

dsMenuPlanner.AcceptChanges()

结束如果

End Sub


这是taIngredients.Update(dsMenuPlanner.Ingredient s)。

崩溃的一行。

传递带删除行的DataRow集合时,错误是更新需要有效的DeleteCommand。


我无法弄清楚如何解决这个。有人可以帮忙吗?


谢谢,

兰迪



兰迪,


我有点想知道你的更新应该是

taIngredients.Update(dsMenuPlanner.bsIngredients)


如果不是那样,而且正如Robin建议的那样,加上比那行前更多

cmb = oledbCommandbuilder(taIngredients)



SQLCommandBuilder(taIngredients)


''取决于你使用的东西
http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx


我希望这会有所帮助,


Cor

" Randy" < sp *********** @ bermail.com的gmail.comschreef

新闻:11 ******************* ***@p77g2000hsh.googlegr oups.com ...




尝试传递表行删除数据源,但是我好了b $ b崩溃了。这是代码:


Private Sub btnDeleteIngr_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理btnDeleteIngr.Click

Dim vbResponse As Integer

vbResponse = MessageBox.Show("你确定要

删除" + tbIngr.Text +"? ;,确认删除,

MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)

如果vbResponse = 1那么

Dim dr As DataRow

Dim drv As DataRowView

drv = bsIngredients.Current

dr = drv.Row

dr.Delete()

bsIngredients.MoveNext()

taIngredients.Update(dsMenuPlanner.Ingredients)

dsMenuPlanner.AcceptChanges()

结束如果

End Sub


这是taIngredients.Update(dsMenuPlanner.Ingredient s)。

崩溃的一行。

传递带删除行的DataRow集合时,错误是更新需要有效的DeleteCommand。


我无法弄清楚如何解决这个。有人可以帮忙吗?


谢谢,

兰迪



谢谢你们的建议。我以为我有一个DeleteCommand

设置,但我做错了。我是在

设计器的表适配器中而不是在属性中完成的。现在修复了,代码

执行没有错误。


我还有一个问题。我没有正确设置CommandText

。我在那里有DELETE FROM Ingredients,其中
显然删除了表格中的所有记录。你能帮我改变这个查询只是删除用户

选择的表吗?它总是会成为当前行。上面的代码

没有改变。如果我能提供更多信息,请告诉我。


再次感谢!

兰迪


Hi,
Trying to pass along a table row delete to the datasource, but I''m
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It''s the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can''t figure out how to resolve this. Can anybody help?

Thanks,
Randy

解决方案

Your data adapter must have InsertCommand, DeleteCommand, and UpdateCommand
objects defined for it in order to perform updates.

Robin S.
--------------------------------
"Randy" <sp***********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...

Hi,
Trying to pass along a table row delete to the datasource, but I''m
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It''s the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can''t figure out how to resolve this. Can anybody help?

Thanks,
Randy



Randy,

I have somehow the idea that your update should be
taIngredients.Update(dsMenuPlanner.bsIngredients)

If it is not that, and it is as Robin suggest, add than before that row
cmb = oledbCommandbuilder(taIngredients)
or
SQLCommandBuilder(taIngredients)

''depending on what you use
http://msdn2.microsoft.com/en-us/lib...ndbuilder.aspx

I hope this helps,

Cor
"Randy" <sp***********@gmail.comschreef in bericht
news:11**********************@p77g2000hsh.googlegr oups.com...

Hi,
Trying to pass along a table row delete to the datasource, but I''m
crashing. Here is the code:

Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDeleteIngr.Click
Dim vbResponse As Integer
vbResponse = MessageBox.Show("Are you sure that you want to
delete " + tbIngr.Text + "?", "Confirm Delete",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
If vbResponse = 1 Then
Dim dr As DataRow
Dim drv As DataRowView
drv = bsIngredients.Current
dr = drv.Row
dr.Delete()
bsIngredients.MoveNext()
taIngredients.Update(dsMenuPlanner.Ingredients)
dsMenuPlanner.AcceptChanges()
End If
End Sub

It''s the "taIngredients.Update(dsMenuPlanner.Ingredient s)" line that
crashes. The error is "Update requires a valid DeleteCommand when
passed DataRow collection with deleted rows."

I can''t figure out how to resolve this. Can anybody help?

Thanks,
Randy



Thank you both for your advice. I thought that I had a DeleteCommand
set up, but I did it wrong. I did it in the table adapter on the
designer rather than in the properties. That''s now fixed and the code
executes without error.

I still have one problem. I haven''t set up the CommandText
correctly. What I have in there is "DELETE FROM Ingredients", which
obviously deletes all records from the table Ingredients. Can you
help me to change this query to just delete the table that the user
has selected? It''s always going to be the current row. The code
above has not changed. Let me know if I can provide more information.

Thanks again!
Randy


这篇关于使用表适配器从数据源中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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