VB.NET Simplying和缩短MySql查询

时间:2014-07-17 03:03:22

标签: mysql vb.net

有一个问题,希望我能从这里学到新东西。

我有这段代码。

Using Con=Openconnetion()
    'Declare MysqlCommand Object
    Dim Cmd as mysqlcommand 

    'Create new instance of mysql command object
    Cmd = New MysqlCommand

    'Inserting Data to TableHead
    Cmd.CommandText = "Insert Into TableHead(doc_no,in_date) values('A','2014-01-01')"
    Cmd.CommandType = CommandType.Text
    Cmd.Connection = Con
    Dim Result As Integer = Cmd.ExecuteNonQuery
    If Result > 0 Then

        'Create new instance of mysql command object
        Cmd = New MysqlCommand

        'If Insertion to TableHead Successfully, Then perform insertion to TableBody
        Cmd.CommandText = "Insert into TableBody(doc_no,item_no,price) values ('A','001','1000')"
        Cmd.CommandType = CommandType.Text
        Cmd.Connection = Con
        Dim Result1 as Integer = Cmd.ExecuteNonQuery
        If Result1 > 0 Then
            MsgBox ("Success")
        Else
            MsgBox ("Failed")

            'Create new instance of mysql command object
            Cmd = New MysqlCommand

            'If Insertion to TableBody Failed, Then Delete The Data From TableHead From Above Query
            Cmd.CommandText = "Delete From TableHead Where doc_no='A'"
            Cmd.CommandType = CommandType.Text
            Cmd.Connection = Con
            Cmd.ExecuteNonQuery
        End If
    End If
End Using

那么,有没有简单的方法来做到这一点,当TableBody的插入返回false状态时,它不会处理查询到TableHead,所以我不需要第3个查询来删除数据TableHead。

0 个答案:

没有答案