如何停止調用函數繼續向下執行
我寫了個調用函數:Public Sub ChkOnly(ByVal sqlstr As String, ByVal chkname As String)
Dim constr As String = My.Settings.constr
Dim con As New SqlConnection(constr)
Dim ds As New DataSet
Try
con.Open()
Dim da As New SqlDataAdapter(sqlstr, con)
da.Fill(ds)
con.Close()
con.Dispose()
If ds.Tables(0).Rows.Count <> 0 Then
MsgBox("已經存在此" & chkname, MsgBoxStyle.Critical, "提示")
Exit Sub
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "提示")
Exit Sub
End Try
End Sub
點擊新增按鈕(btn_add)觸發的執行函數,新增前,我檢查數據庫表裡是否已經存在同樣的數據.代易做图如下:
Dim sqlstr_chk As String = "select ply from MTPm where MType='" & Trim(Me.Cbx_MMtype.Text) & "'"
ChkOnly(sqlstr_chk, "機種")
Dim sqlstr As String = "insert into MTPm values('" & Trim(Me.Cbx_MPly.Text) & "','" & Trim(Me.Cbx_MMtype.Text) & "'," & Val(Trim(Me.Tbx_FstckAlarm1.Text)) & _
"," & Val(Trim(Me.Tbx_FstckAlarm2.Text)) & "," & Val(Trim(Me.Tbx_OQCAlarm1.Text)) & "," & Val(Trim(Me.Tbx_OQCAlarm2.Text)) & "," & Val(Trim(Tbx_pdct_x1.Text)) & "," & Val(Trim(Tbx_pdct_x2.Text)) & ")"
sqlexcute(sqlstr, "新增機種")
''''''sqlexcute()是執行SQL語句的調用函數.
在執行ChkOnly()要新增的數據已經存在時候,停止執行調用函數下面的代易做图,即 :
dim sqlstr,sqlexcute()....
請問這樣的功能怎麼實現啊?我分沒了,請好心的高手幫忙指點一下.
--------------------编程问答-------------------- 可以在前面加個判斷數據是否存在,再新增。。。
if 條件 then
存在則:
else
不存在
end if --------------------编程问答-------------------- 我觉得可以把ChkOnly这个函数改写一下,改成直接返回一个DataTable
然后
Dim sqlstr_chk As String = "select ply from MTPm where MType='" & Trim(Me.Cbx_MMtype.Text) & "'"
dim dt as DataTable=ChkOnly(sqlstr_chk, "機種")
if dt.Tables(0).Rows.count=0 Then
Dim sqlstr As String = "insert into MTPm values('" & Trim(Me.Cbx_MPly.Text) & "','" & Trim(Me.Cbx_MMtype.Text) & "'," & Val(Trim(Me.Tbx_FstckAlarm1.Text)) & _
"," & Val(Trim(Me.Tbx_FstckAlarm2.Text)) & "," & Val(Trim(Me.Tbx_OQCAlarm1.Text)) & "," & Val(Trim(Me.Tbx_OQCAlarm2.Text)) & "," & Val(Trim(Tbx_pdct_x1.Text)) & "," & Val(Trim(Tbx_pdct_x2.Text)) & ")"
sqlexcute(sqlstr, "新增機種")
else
messagebox.show("机种已存在!")
Cbx_MMtype.focus()
exit sub
补充:.NET技术 , VB.NET