Form 1
codice:
Public Class anagraficaCF
Public Sub btnSalva_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalva.Click
If tbxRagioneSociale.Text = String.Empty Then
MsgBox("Impossibile inserire i dati nell'archivio. Controllare che il campo Ragione Sociale non sia vuoto", MsgBoxStyle.Critical, "Impossibile effettuare l'operazione")
Else
Dim anagrafica As New classi.anagraficaClienteFornitore
anagrafica.inserisci(tbxRagioneSociale.Text)
End If
End Sub
End Class
classi
codice:
'libreria per usare mysql
Imports MySql.Data.MySqlClient
Public Class classi
Class anagraficaClienteFornitore
Public Sub connessione()
Dim ServerString As String = "Server=localhost;User Id=root;Password=password;Database=db"
Dim SQLConnection As MySqlConnection = New MySqlConnection
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
'MsgBox("Connessione con il server effettuata con successo")
Else
SQLConnection.Close()
MsgBox("Error. Control connection parameter")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub inserisci(ByRef ragioneSociale As String)
Dim ServerString As String = "Server=localhost;User Id=root;Password=password;Database=db"
Dim SQLConnection As MySqlConnection = New MySqlConnection
SQLConnection.ConnectionString = ServerString
SQLConnection.Open()
Dim SQLStatement As String = "INSERT INTO anagraficafornitori(ragioneSociale) VALUES('" & ragioneSociale & "')"
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
MsgBox(ragioneSociale & " aggiunto con successo")
SQLConnection.Dispose()
End Sub
End Class
End Class
così funziona, ma vorrei non ripetere la connessione