|
我有以下table的SQL Server称为tblProducts:! _0 m. G( _! F( Q4 @
+-----------+--------------+-------------+1 i+ I2 S2 i7 v$ n2 t
| pkProduct | fkProductID | intIssue |
; } ^4 d, R& u% S8 U0 [+-----------+--------------+-------------+' K" f' \' ], N. V! H% S
| 1 | 10 | 1 |
6 Y x& j' n+ X9 x7 a' i| 2 | 10 | 2 |
* u4 O1 m+ U/ \$ ?3 K; R| 3 | 10 | 4 |
: j( f8 U+ X4 O0 f4 N& f| 4 | 11 | 1 |% [! O( I8 u: e
| 5 | 11 | 2 |, I. D% _+ A' ^
| 6 | 11 | 3 |
/ W7 s4 n S/ S| 7 | 11 | 5 |$ S& q; c8 |4 g: T: Z
| 8 | 12 | 1 |3 J9 `4 I9 P9 k9 T6 ]9 |9 k
| 9 | 13 | 1 |
! J) I2 Y2 x6 T' K: g| 10 | 13 | 4 |4 z7 M2 ]7 B R$ _' z
| 11 | 14 | 1 |
* Y2 r! g: o& q: W3 `/ S| 12 | 14 | 3 |
8 n* O0 w$ b& _; c, ?! s| 13 | 14 | 6 |8 ^/ F; n( x/ j- q2 s
| 14 | 15 | 1 |6 U v# K# ?% }8 ~, U. N
| 15 | 16 | 1 |
" |# x7 b/ P, P- D( T1 }- ~+-----------+--------------+-------------+2 r) d3 W( R2 b3 v% @4 L
随着时间的流逝,由于各种原因删除了行,现在问题编号中就有空白。我希望问题编号按顺序运行,table如下所示: % f; n/ W0 U: h# P' Y8 O" H
+-----------+--------------+-------------+
' X1 V) |+ B( K t! Y. T/ _7 L| pkProduct | fkProductID | intIssue |& K' i' F1 G4 c0 J2 [2 R
+-----------+--------------+-------------+
! `* t+ Q/ X' Q9 r| 1 | 10 | 1 |
0 R/ \6 Z8 O P/ s' T| 2 | 10 | 2 |
0 [3 Z' R0 X; k) D% W| 3 | 10 | 3 |( d/ e0 C: i1 d
| 4 | 11 | 1 |2 ]9 ]6 N6 i: M: h$ s7 W( S- G0 M
| 5 | 11 | 2 |
' k" O) H3 H) l| 6 | 11 | 3 |6 S; K+ [+ u& R% ^
| 7 | 11 | 4 |5 o7 N/ r, B9 t; D: {2 T m9 f' ^6 `
| 8 | 12 | 1 |! w3 ~9 S! l5 N/ D1 `. Y
| 9 | 13 | 1 |
( f2 L% x9 d }, z2 {| 10 | 13 | 2 |
/ n9 H7 ?. ^% ^: |) l| 11 | 14 | 1 |
+ q1 l# {; e0 y' i+ Y( |% }| 12 | 14 | 2 |
8 Z+ h# N# r) t" U9 L0 G% Q| 13 | 14 | 3 |9 m3 j! }/ [# ?0 k( G
| 14 | 15 | 1 |/ y: o% F* Y4 O( m
| 15 | 16 | 1 | Y, Q. t5 f2 J6 h3 V
+-----------+--------------+-------------+; z8 K+ `( e, p( M1 {/ e4 ?
目前,我已经添加了table,DataSet并在vb.net代码中使用此代码来更新行: 3 q8 ~" R8 @0 q2 T5 D0 w
Dim currentProductsTable As tblProductsDataTable9 q8 |! u" O R& q
Using taProduct As New tblProductsTableAdapter
: m, L4 H# z' d! B4 I. D& k currentProductsTable = taProduct.GetData+ w' {0 P0 [ r# ]7 O
End Using
- g% ^" k, u. {' q0 |' IDim issueCounter As Integer = 1
% b% @# w8 @7 [* n) ~5 O3 aDim previousRow As tblProductsRow = Nothing
7 @' ]8 u0 @3 X1 Y7 oUsing con As SqlConnection = New SqlConnection(My.Settings.MyConnectionString)
$ }: E, i/ G8 s; f4 V Using cmd As New SqlCommand("UPDATE dbo.tblProducts SET [intIssue] = @issueCounter WHERE pkProduct = @ProductID", con)( j. |. ^. D2 L1 t s6 ~/ s; o' d
cmd.Connection = con . E8 B) x! b- z# ]) b
con.Open(); P/ _: C0 M, Q
For Each row As tblProductsRow In currentProductsTable.Rows2 _' |! n8 |! S; D5 u7 J% j
If Not previousRow Is Nothing Then T& F2 \( ]/ f4 W/ ]# `
If row.fkProductID = previousRow.fkProductID Then
. Q7 T9 ~2 |4 S" U4 N cmd.Parameters.Clear()
$ r0 J- s6 X2 G cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter' j$ @3 T1 W* g+ g0 Y7 [. c
cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct+ m3 }. |& i$ m. P
previousRow = row2 v" y6 Q3 V- M
Else4 x3 k2 G9 U* ?: B, m$ R
issueCounter = 1
: {5 i, P+ p- u2 G* x' Y/ o cmd.Parameters.Clear()
# ^1 \9 i4 y X4 i: B6 {: S, @1 |0 i cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter2 s% P/ N( D/ X& O* ~
cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct2 s3 ]1 `1 }/ W, c1 w2 s
previousRow = row5 o5 j* n4 D1 Z" y$ b. O2 c
End If
+ I9 f5 ~2 B( h/ b* f Else3 S/ R; n$ y. Z6 T2 Z9 i2 z) m
issueCounter = 1
9 [% F' n6 d: b1 V8 H% V cmd.Parameters.Clear()
! \' H' f; s3 _6 B4 H cmd.Parameters.Add("@issueCounter", SqlDbType.Int).Value = issueCounter) a7 g: {6 D, }, l! N2 G0 z
cmd.Parameters.Add("@ProductID", SqlDbType.Int).Value = row.pkProduct& I% C- q9 J3 y$ ~
previousRow = row; Y" I/ r7 a$ D" |: W; A3 v6 e
End If; T( K' t8 K P( v! y
cmd.ExecuteNonQuery()6 @3 D. V" U1 o4 Z& _
issueCounter += 1
3 a% i8 N* A' i7 M& }" F# B' t Next
. \0 x- T) X# c Q End Using
7 v& e! ~- y: ]4 p1 k% TEnd Using
$ J, r% k3 Z C# I( T9 I我已下令.GetData由fkProductID ASC,和intIssue2 y) n7 F, _0 c, r$ o0 g8 d3 y
ASC,但它需要为大约有30000很长一段时间rows的table。
' }; e# |7 m7 e: @我想知道是否有更快的方法来进行这种更新?8 W. B( H/ c2 X5 J7 e. I
E% x- j) ~2 S* o) \% u$ [: u
解决方案: |
|