在vb.net中的多个表中添加数据

时间:2010-03-20 05:55:33

标签: database vb.net

这是一个winform,我使用mysql作为数据库,这是我的代码: 我正在尝试将数据添加到多个表中。

If TextBox14.Text = "" Or TextBox7.Text = "" Or TextBox10.Text = "" Then
            MsgBox("Please fill up the fields with a labels in bold letters!", MsgBoxStyle.Information)


            cn = New MySqlConnection("Server=localhost; Database=school;Uid=root;Pwd=nitoryolai123$%^;")
            'provider to be used when working with access database
            cn.Open()
            cmd = New MySqlCommand("select * from parents, mother, father", cn)



            cmd.CommandText = "insert into parents values('" + idnum + "','" + p_contact + "','" + p_ad + "')"
            cmd.CommandText = "insert into mother values('" + idnum + "','" + mother + "','" + mother_occu + "')"
            cmd.CommandText = "insert into  father values('" + idnum + "','" + father + "',''" + father_occu + "')"


            cmd.ExecuteNonQuery()

我收到此错误,请帮忙: 索引和长度必须指向字符串中的位置。参数名称:长度

2 个答案:

答案 0 :(得分:0)

    Dim sql As String
    sql = "insert into parents (`id`,`contact`,`ad`) values('" + idnum + "','" + p_contact + "','" + p_ad + "')"
    sql = sql & "; insert into mother (`id`,`mom`,`occ`) values('" + idnum + "','" + mother + "','" + mother_occu + "')"
    sql = sql & "; insert into father (`id`,`dad`,`occ`) values('" + idnum + "','" + father + "',''" + father_occu + "')"
    cmd.CommandText = sql

答案 1 :(得分:0)

我认为你必须这样做

cmd = New MySqlCommand()

 cmd.CommandText = "insert into parents values('" + idnum + "','" + p_contact + "','" + p_ad + "')"  

            cmd.ExecuteNonQuery() 

            cmd.CommandText = "insert into mother values('" + idnum + "','" + mother + "','" + mother_occu + "')" 

           cmd.ExecuteNonQuery() 

            cmd.CommandText = "insert into  father values('" + idnum + "','" + father + "',''" + father_occu + "')"    

            cmd.ExecuteNonQuery()