老师,下面的这段代码中,
cmd2.commandText = "Updat e [编号]
Set [顺序号] = "
&
(Key + 1) &
" Where [顺序号] = "
&
Key
&
" And [前缀] = '"
&
pf
&
"'" 这条语句肯定会执行成功的,也就是cmd2.ExecuteNonQuery() > 0 肯定会成立的,为什么还要用 DO......LOOP 循环呢?而且DO......LOOP又没有判断条件,如果cmd2.ExecuteNonQuery() > 0不成立的话,那程序不是死循环了??麻烦老师解释下,谢谢啦!!
Dim dr As
DataRow = e.DataRow
Dim pf As
String
If
dr.RowState <> DataRowState.Added Then
'如果不是新增行
Return
'那么返回
ElseIf dr.IsNull("日期") Then
'如果没有输入日期
e.Cancel = True
'取消保存此行
MessageBox.Show("必须输入日期!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
Return
Else
pf =
Format(dr("日期"),"yyMM") '否则获得编号的前缀,两位年,两位月
End
If
Dim cmd1 As New SQLCommand
Dim
cmd2 As New SQLCommand
Dim Key As
Integer
cmd1.ConnectionName = "编号"
'设置数据源名称
cmd2.ConnectionName = "编号"
cmd1.commandText = "Select Count(*) Fro m [编号] Where [前缀] =
'" & pf & "'"
If cmd1.ExecuteScalar = 0 Then '如果编号表不存在前缀的行,那么增加一行
cmd1.commandtext = "Inser t Into 编号 (前缀, 顺序号) Values('"
&
pf
&
"',1)"
cmd1.ExecuteNonQuery
End If
cmd1.commandText = "Selec t [顺序号]
Fro m [编号] Where [前缀] = '"
&
pf
&
"'"
Do
Key = cmd1.ExecuteScalar() '从后台获得顺序号
cmd2.commandText = "Updat e [编号]
Set [顺序号] = "
&
(Key + 1) &
" Where [顺序号] = "
&
Key
&
" And [前缀] = '"
&
pf
&
"'"
If cmd2.ExecuteNonQuery() > 0 Then
'更新顺序号
Exit Do '更新成功则退出循环
End If
Loop
e.DataRow("编号") =
pf & "-" & Format(Key,"0000")