更新绑定到datagridview的数据集
在我的datagridview中,绑定到数据集的用户可以进行一些更改。并非用户可以更改数据集中的所有字段。
In my datagridview which is bound to dataset users can make some changes. Not all fields from dataset are possible to be changed by users.
一切都是我的DAL类
Everything is my DAL class
这是我用来绑定datagridview的东西:
This is what i use to bind datagridview:
Public ReadOnly GetArtikelDataSet As New DataSet
Public Function GetDataSet() As DataSet Implements IDbManipulation.GetDataSet
Using con As New SqlConnection(_strcon)
Using cmd As New SqlCommand("SELECT Art.Id, Art.Nummer, Art.Serie, Art.EANBarcode, Art.Preis, Art.[User], Art.Vater, Art.Name, Art.Amazon, Kat.Name As Kategorie, Subkat.Name As Subkategorie, ISNULL(Subsubkat.Name,'') As SubSubkategorie, Art.FK_Geschaft_ID, Art.CreateDate FROM T_Article As Art " &
"INNER JOIN T_Kategorie As Kat ON Art.FK_Kategorie_ID=Kat.Id " &
"INNER JOIN T_Subkategorie As Subkat ON Art.FK_SubKategorie_ID=Subkat.Id " &
"LEFT OUTER JOIN T_SubSubKategorie As Subsubkat ON Art.FK_SubSubKategorie_ID=Subsubkat.Id ORDER BY Art.Vater ASC",
con) ' order by
con.Open()
Using getProjectsDataAdapter = New SqlDataAdapter(cmd)
getProjectsDataAdapter.Fill(GetArtikelDataSet, eArticle.Main.ToString)
End Using
End Using
End Using
Return GetArtikelDataSet
End Function
这是我检查dasaource是否有任何变化的方法:
This is how i check whether any change to dasaource:
Public Function CheckChangesDataSet() As Boolean
Dim result = False
If GetArtikelDataSet.HasChanges Then
result = True
End If
Return result
End Function
如果上面的方法给我真实,那么我这样做:
If above method gives me true then i do this:
Public Sub MakeChangesDataSet() Implements IDbManipulation.MakeChangesDataSet
If GetArtikelDataSet.Tables(0).Rows.Count > 1 Then
End If
Using myConnection = New SqlConnection(_strcon)
Using cmd As New SqlCommand("SELECT Id, Serie, EANBarcode, Preis, Name, Amazon, FK_Geschaft_ID FROM T_Article ORDER BY ID ASC",
myConnection)
myConnection.Open()
'Create a data adapter in the method and throw it away afterwards
Using getProjectsDataAdapter = New SqlDataAdapter(cmd)
Dim cmdbuilder As New SqlCommandBuilder(getProjectsDataAdapter)
getProjectsDataAdapter.Update(GetArtikelDataSet, eArticle.Main.ToString)
End Using
End Using
End Using
End Sub
至点:
-
是否正确,因为我在MakeChangesDataSet()中只有字段可以被网格中的用户更改,或者我必须输入与GetDataSet完全相同的查询?
-
Is it correct as i did that in MakeChangesDataSet() having just fields which could be changed by users in grid or do i have to type exactly the same query as was in GetDataSet?
>
两个方法都应该:GetArtikelDataSet和MakeChangesDataSet共享相同的SqlDataAdapter吗?正如你所看到的,我对一个方法和另一个方法使用了diffrent。不应该在类级别定义和共享适配器,这样两个方法都可以访问它们吗?
Should both methods: GetArtikelDataSet and MakeChangesDataSet share same SqlDataAdapter? As you see i use diffrent for one method and another. Shouldn't adapter be defined and shared on class level so both methods access the same?
不确定但是读取了一个Updatecommand必须与sqldataadapter相关但不知道它是什么意思的地方。
Not sure but read somwhere that Updatecommand has to be associated with sqldataadapter but have no idea what does it mean.
我听说即使我在数据适配器上更新它也可以选择插入/更新/删除 - 在我的情况下网格可能只是修改(没有可能用户插入行或删除)i  ;想(为了限制sqladapter进行插入/删除)
如何做到这一点?
I heard that even if i do Update on data adapter it could choose either insert/update/delete - in my case grid could be just modified (no possibility to insert rows by user or delete) i would like (just in case to restrict sqladapter to make insert/delete) how to do that?
主要询问所有这些因为有时似乎不是每次都更新数据。
$
Mainly asking all of this because sometimes seems that not every time data is updated.
不考虑适配器。
Not thinking about an Adapter.
- 仅限更新有关用户可以更改的允许字段需要更新的内容。
- 无论哪种方式,我使用不同的对象来处理数据,例如连接和命令,它们是本地的(在一种方法中)到手头的任务,例如更新/插入/删除/读取。
- 似乎正确但只有你走这条路。
- 如果你使用的是连接/命令,T-SQL有MERGE INTO(检查出来)
- Only update what needs to be updated in regards to allowable fields that the user can change.
- Either way, I use different objects for working with data e.g. a connection and command where they are local (in one method) to the task at hand e.g. update/insert/delete/read.
- Seems correct but only if you go this route.
- If you were using a connetion/command T-SQL has MERGE INTO (check it out)