VB.NET和MySql保存到两个表;父表和子表

时间:2018-03-22 13:55:51

标签: mysql vb.net

这里我试图将信息保存到两个表(使用vb.net); students(父表)和guardians(子)。已创建表之间的关系,外键(guardian id)自动递增。以下是我的代码,我们将非常感谢您的帮助。

    Dim cn As New MySqlConnection
    Dim cmd As New MySqlCommand
    Dim dr As MySqlDataReader

    cn.ConnectionString = "Server=localhost; user id='root'; password='' ; database='dbname'"
    cmd.Connection = cn
    cn.Open()
    cmd.CommandText = "Select stud_id, firstname, lastname, dob, sickness, sex, pin, payment_type, level_stream FROM students WHERE stud_id = '" & txtstud_id.Text & "'"
    'cmd.CommandText = "Select guardian_id, firstname1, lastname1, sex1, occupation, relationship, address, cell, telephone, email FROM guardians WHERE guardian_id = '" & txtgid.Text & "'"
    dr = cmd.ExecuteReader

    If dr.HasRows Then
        MsgBox("Student ID already exist!", MsgBoxStyle.Critical, "Checkpoint")
    Else
        cmd.Dispose()
        dr.Dispose()
        cmd.CommandText = " Insert into students (stud_id, firstname, lastname, dob, sickness, sex, pin, payment_type, level_stream) Values ('" & txtstud_id.Text & "','" & txtfname.Text & "','" & txtlname.Text & "','" & DateTimePicker1.Text & "','" & txtsickness.Text & "','" & cmbsex.Text & "','" & txtpin.Text & "','" & cmbpay_opt.Text & "','" & cmblevel_stream.Text & "')"
        'cmd1.CommandText = " Insert into guardians (guardian_id, firstname1, lastname1, sex1, occupation, relationship, address, cell, telephone, email) Values ('" & txtgid.Text & "','" & txtfname1.Text & "','" & txtlname1.Text & "','" & cmbsex1.Text & "','" & txtoccupation.Text & "', '" & txtrelationship.Text & "','" & txtaddress.Text & "', '" & txtcell.Text & "','" & txttel.Text & "','" & txtemail.Text & "')"

        cmd.ExecuteNonQuery()
        MsgBox("Information successfully saved", MsgBoxStyle.Information, "Saving data succeed")
        txtstud_id.Clear()
        txtstud_id.Focus()
        txtfname.Clear()
        txtlname.Clear()
        DateTimePicker1.Text = String.Empty
        cmbsex.Text = String.Empty
        txtpin.Clear()
        txtsickness.Clear()
        txtgid.Clear()
        txtfname1.Clear()
        txtlname1.Clear()
        cmbsex1.Text = String.Empty
        txtoccupation.Clear()
        txtrelationship.Clear()
        cmbpay_opt.Text = String.Empty
        txtaddress.Clear()
        txtcell.Clear()
        txttel.Clear()
        txtemail.Clear()
        cmblevel_stream.Text = String.Empty

    End If

1 个答案:

答案 0 :(得分:0)

以下是根据我在评论中所说的单独执行的示例 请阅读代码段中的注释。

Dim cn As New MySqlConnection
//''1st Commnad variable
Dim cmd As New MySqlCommand
 //''2nd Commnad variable
Dim cmd1 As New MySqlCommand
 //''3rd Commnad variable
Dim cmd2 As New MySqlCommand

Dim dr As MySqlDataReader

cn.ConnectionString = "Server=localhost; user id='root'; password='' ; database='dbname'"

cmd.Connection = cn

cn.Open()

cmd.CommandText = "Select stud_id, firstname, lastname, dob, sickness, sex, pin, payment_type, level_stream FROM students WHERE stud_id = '" & txtstud_id.Text & "'"

dr = cmd.ExecuteReader

If dr.HasRows Then
    MsgBox("Student ID already exist!", MsgBoxStyle.Critical, "Checkpoint")
    cmd.Dispose()
    dr.Dispose()
    cn.Close()
Else
    cmd.Dispose()
    dr.Dispose()
    //'In this case you don't want to close the connection because you executing another queries too. 
    //'otherwise you have to check connection status and do open or close according to the status.
    //'For Example
    //'If (cn.State = ConnectionState.Closed)Then
    //' cn.Open()
    //'End if



    cmd1.Connection = cn
    cmd1.CommandText = " Insert into students (stud_id, firstname, lastname, dob, sickness, sex, pin, payment_type, level_stream) Values ('" & txtstud_id.Text & "','" & txtfname.Text & "','" & txtlname.Text & "','" & DateTimePicker1.Text & "','" & txtsickness.Text & "','" & cmbsex.Text & "','" & txtpin.Text & "','" & cmbpay_opt.Text & "','" & cmblevel_stream.Text & "')"
    cmd1.ExecuteNonQuery()
    cm1.Dispose()


    cmd2.Connection = cn
    cmd2.CommandText = " Insert into guardians (guardian_id, firstname1, lastname1, sex1, occupation, relationship, address, cell, telephone, email) Values ('" & txtgid.Text & "','" & txtfname1.Text & "','" & txtlname1.Text & "','" & cmbsex1.Text & "','" & txtoccupation.Text & "', '" & txtrelationship.Text & "','" & txtaddress.Text & "', '" & txtcell.Text & "','" & txttel.Text & "','" & txtemail.Text & "')"
    cmd2.ExecuteNonQuery()
    cmd2.Dispose()


    //'Close when its over. otherwise you have to check whether connection is open or not before you start trans queries to database server. See else statement comments for example.
    cn.Close()

    MsgBox("Information successfully saved", MsgBoxStyle.Information, "Saving data succeed")
End If
//'Disposing cmd and dr

您可以使用相同的命令,但您必须考虑如何使用它。执行命令或datareader后,必须对其进行Dispose。更多关注连接属性为Closed或Open。​​