Best practice for updates to a single datatable row
Why does the first method below work? I would have thought that the variable 'row' is completely separate from anything else, such as a row in DataTable1. Also, is there a drawback to using the first method? For changes to lots of columns in a single row, I'd rather do it that way.
dim row = Me.DataSet1.开发者_运维问答DataTable1(0)
row.Item1=123
DataTable1TableAdapter.Update(Me.DataSet1.DataTable1)
OR
DataSet1.DataTable1(0).Item1=123
DataTable1TableAdapter.Update(Me.DataSet1.DataTable1)
I believe setting row the way you are it is using vb .net type inference. when you set it equal to Me.DataSet1.DataTable1(0) any changes made to row will act as reference to Me.DataSet1.DataTable1(0). @Bala you very right they are equivalent.
If i can interject personal preference i would definitely go with option two, no need to use a needless variable, and it's obviously cleaner and more straight forward to read. AS far as a best practice, if you don't need the variable don't initialize it. especially one that directly references a value in your Table. Hope this helps.
The variable 'row' is being set to the first row of table1. Therefore, when you update table1, it pushes any changes made to 'row'.
精彩评论