GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2008, 10:05
carcleo carcleo is offline
New Member
 
Join Date: Oct 2008
Location: Muriaé-MG - Brasil
Posts: 4
carcleo is on a distinguished road

Need to use a variable in a class


I am needing the your help for a question:

i am doing a aplication on VB.Net for windows and be with a doubts.

i have the code down were i have a class call DBConnection for to talk with MySql databases
VB / Visual Basic Code:
Imports MySql.Data.MySqlClient
'Comentando varias linhas Ctrl + K + C
'Descomentando varias linhas Ctrl + K + U
Public Class DBConnection
    Dim Conexao As MySqlConnection
    Dim cmd As MySqlCommand = New MySqlCommand()
    Public Sub New()
        Conexao = New MySqlConnection()
    End Sub

    Public Sub OpenConnection()
        Dim strConnString As String = "server=localhost;userid=root;password=;database=Vendas"

        If Conexao Is Nothing Or Conexao.State = ConnectionState.Broken Or Conexao.State = ConnectionState.Closed Then
            Conexao = New MySqlConnection()
            Conexao.ConnectionString = strConnString
            Conexao.Open()
        End If

    End Sub

    Public Sub CloseConnection()
        If Conexao.State = ConnectionState.Open Then
            Conexao.Close()
            Conexao.Dispose()
            Conexao = Nothing
        End If
    End Sub

    Public Function ExecuteDS(ByVal sql As String, ByRef tabela As String) As DataSet
        Dim ds As New DataSet
        Dim da As MySqlDataAdapter = New MySqlDataAdapter()

        Try
            OpenConnection()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = sql
            cmd.Connection = Conexao
            da.SelectCommand = cmd
            da.Fill(ds, tabela)
            da.Dispose()
            Return ds
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return ds
    End Function

End Class

I need to use the variable "conexao" that in this class DBConnection. Use it in the code below see:
VB / Visual Basic Code:
Private Sub incluirRegistro(ByVal dr As DataRow)
        Dim reg As Integer
        Dim Conexao As MySqlConnection
        Dim comando As MySqlCommand
        Try
            Conexao = New MySqlConnection("server=localhost;uid=root;pwd=;database=vendas")
            Conexao.Open()
            comando = New MySqlCommand("INSERT INTO Clientes (Nome,Endereco,Email)VALUES ('" & Nome.Text & "','" & Endereco.Text & "','" & Email.Text & "')", Conexao)   '<<--here i need to use the variable of the class DBConection
            reg = comando.ExecuteNonQuery()

            MessageBox.Show(reg.ToString + " registro de Usuário incluido com sucesso.")
        Catch ex As MySqlException
            MessageBox.Show("Erro ao efetuar a conexão com a base de dados : " + ex.Message)
        Finally
            '   Conexao.Close()
        End Try
end sub
how to do this?
Last edited by admin : 27-Oct-2008 at 16:34. Reason: Please insert your example VB codes between [VB] and [/VB] tags
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:52.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.