Jump to content
ricardom94

Erro - cmd.ExecuteNonQuery()

Recommended Posts

ricardom94

Eu tenho o mesmo erro o programa diz me que se ligou a bd mas quando vou a inserir um registo ele dame erro no mesmo sitio

Imports MySql.Data.MySqlClient


Public Class Form1
    Dim serverstring As String = "Server=localhost;Userid=root;Password=;Database=test"
    Dim sqlcon As MySqlConnection = New MySqlConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sqlcon As MySqlConnection = New MySqlConnection
        sqlcon.ConnectionString = serverstring

        Try
            If sqlcon.State = ConnectionState.Closed Then
                sqlcon.Open()
                MsgBox("Ele diz que se ligou a bd")
            Else
                sqlcon.Close()
                MsgBox("Ele diz que nao se ligou a bd")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub inserir(ByRef sqlstatement As String)
        Dim cmd As MySqlCommand = New MySqlCommand(sqlstatement, sqlcon)

        With cmd
            .CommandText = sqlstatement
            .CommandType = CommandType.Text
            .Connection = sqlcon
            .ExecuteNonQuery()
        End With
        sqlcon.Close()
        MsgBox("Ele diz que inseriu")
        sqlcon.Dispose()


    End Sub

    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        Dim sqlstatement As String = "INSERT INTO tt(krl) VALUES('" & cod.Text & "')"
        inserir(sqlstatement)
    End Sub

e depois diz me isto do executenonquery

erroks.png

agradecia alguma dica sff

Share this post


Link to post
Share on other sites
José Lopes

Eu ia sugerir que testasses a conexão antes de cada inserção....

E obviamente fechá-a no final ;-)


Quando te pedirem peixe.... ensina-os a Pescar!!Hum..lálálálá!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.