问题描述
大家好,我有一个带有SQL Server 2014的数据库。我制作了一个选择多个字段的存储过程。在那之前一切都很好。它返回一个我与datagridview绑定的数据表。问题是当我想要更新该数据表时。我为更新做了另一个存储过程。 (我的更新在sql server中正常工作)。出于某些原因,当我从visual basic进行更新时,他确实更新了我的数据集,但没有更新我的数据库!由于某个原因x,它看起来好像没有连接到数据库。
我知道我可以使用sqlconnection.open()和sqlconnection.close()但我使用Disconnected Ado.net,所以当我调用updatecommand时,它应该直接连接到数据库!但事实并非如此!我做错了什么?
Ps:抱歉我的英语不好。
这是一部分我的代码:
我尝试了什么:
Hi guys, i have a database with SQL server 2014. I made a stored procedure that selects several fields. Until then everything is fine. it returns a datatable that I bind with my datagridview. The problems is when i want to update that datatable. I made an other stored procedure for the update. (My update work fine in sql server). For some reasons when I do the update from visual basic, he did update my dataset but not my database! For a reason x, it looks like it does not connect to the database.
I know i can use sqlconnection.open() and sqlconnection.close() but im working with the "Disconnected Ado.net" so when i call the "updatecommand", it's supposed to connect directly to the database! But it didn't! What am i doing wrong?
Ps: Sorry for my bad english.
Here is a part of my code:
What I have tried:
'Initialize my command
command.CommandType = CommandType.StoredProcedure
command.CommandText = "dbo.update"
command.Parameters.Add(New SqlParameter("@idInventory", SqlDbType.Int, 1, table.Columns(0).Caption))
command.Parameters.Add(New SqlParameter("@idProduct", SqlDbType.Int, 1, table.Columns(1).Caption))
command.Parameters.Add(New SqlParameter("@code", SqlDbType.NVarChar))
command.Parameters("@code").SourceColumn = "code"
command.Connection = adaptater.Connection
Try
adaptater.Adapter.UpdateCommand = command
Catch ex As Exception
MsgBox(ex.Message)
End Try
推荐答案
command.Connection = adaptater.Connection
尝试
adaptater.A dapter.UpdateCommand = command
Catch ex As Exception
MsgBox(ex.Message)
结束尝试
command.Connection = adaptater.Connection
Try
adaptater.Adapter.UpdateCommand = command
Catch ex As Exception
MsgBox(ex.Message)
End Try
嗯,这是你的问题。您已将命令分配给适配器的 UpdateCommand
属性,但您从未告诉适配器更新任何内容!
您需要调用适配器的更新
方法,并传入要更新的 DataSet
。您可能还需要初始化适配器的 InsertCommand
和 DeleteCommand
属性,具体取决于您是否允许用户创建新行并删除现有行。
[]
Well, there's your problem. You've assigned the command to the adapter's UpdateCommand
property, but you've never told the adapter to update anything!
You need to call the adapter's Update
method, passing in the DataSet
that you want to update. You might also need to initialize the adapter's InsertCommand
and DeleteCommand
properties, depending on whether you allow the user to create new rows and delete existing rows.
Updating Data Sources with DataAdapters[^]
这篇关于使用强类型数据集更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!