Jump to content
Fórum Script Brasil
  • 0

Adicionar novos dados em banco de dados


igordeeoliveira
 Share

Question

Estou com um problema em adicionar/editar/deletar novos itens na tabela, eu segui um tutorial e me levou ao código abaixo e funciona, ele lista os valores na combobox, mas ai eu clico em adicionar e abr e o form2 com dois textbox

form1.vb:

Imports System.Data
Imports System.Data.OleDb
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PPreenchaComboBox()
    End Sub

    Private Sub PPreenchaComboBox()
        Using con As OleDbConnection = GetConnection()
            Try
                con.Open()
                Dim sql As String = "SELECT Nome FROM servidores"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)
                Dim da As OleDbDataAdapter = New OleDbDataAdapter
                Dim dt As DataTable = New DataTable
                da.SelectCommand = cmd
                da.Fill(dt)

                cmxNomeservidores.DisplayMember = "Nome"
                cmxNomeservidores.DataSource = dt
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                con.Close()

            End Try
        End Using
    End Sub

    Private Sub PleDados()
        Dim dr As OleDbDataReader = Nothing

        Using con As OleDbConnection = GetConnection()
            Try
                con.Open()
                Dim sql As String = "SELECT IP FROM servidores WHERE nome='" & cmxNomeservidores.Text & "'"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, con)

                dr = cmd.ExecuteReader(CommandBehavior.SingleRow)

                If dr.HasRows Then
                    dr.Read()
                    lbl_IP.Text = dr.Item("IP")
                Else
                    MsgBox("Não foi encontrado")
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                con.Close()
            End Try
        End Using
    End Sub


    Private Sub cmxNomeservidores_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmxNomeservidores.SelectedIndexChanged
        PleDados()
    End Sub
    Private Sub btn_addservidor_Click(sender As Object, e As EventArgs) Handles btn_addservidor.Click
        Form2.Show()
    End Sub
End Class

mdlAcesso.vb:

Imports System.Data
Imports System.Data.OleDb
Module mdlAcesso

    Public Function GetConnection() As OleDbConnection
        Dim sql As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\bancoservidores.accdb"
        Return New OleDbConnection(sql)
    End Function
End Module
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Forum Statistics

    • Total Topics
      149.8k
    • Total Posts
      646.7k
×
×
  • Create New...