Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Data - SQL class

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] [2005] Data - SQL class

    Hi,

    I'm coding my 1st class in VB2005.
    This class that I've coded is going to be used to connect, disconnect and to run SQL commands.

    By now, I just can't execute succesfully any kind of query. This query is executed in the 2nd code quote.

    Could anybody take a look to my code?

    Code:
    Public Class acessos
        Public CNN As New Data.SqlClient.SqlConnection
        'Public rs As New Data.SqlClient.SqlDataAdapter
    
        Public Const ID_DB As String = "TLERP"
        Public Const server As String = "TURION"
        Public Const user As String = "xx"
        Public Const Password As String = "xx"
    
        Public Function Conectar() As Boolean
            Try
                CNN.ConnectionString = "Server=" & server & ";Database=" & ID_DB & ";User ID=" & user & ";Password=" & Password & ";Trusted_Connection=False"
                CNN.Open()
                Conectar = True
    
                Exit Function
    
            Catch err As Exception
                Conectar = False
            End Try
    
        End Function
    
        Public Function Desconectar() As Boolean
            Try
                CNN.Close()
                CNN = Nothing
                Desconectar = True
    
                Exit Function
    
            Catch err As Exception
    
                Desconectar = False
            End Try
    
        End Function
        Public Function AbrirDataAdapter(ByVal SqlString As String) As Data.SqlClient.SqlDataAdapter
    
            Dim da As Data.SqlClient.SqlDataAdapter
    
            da = New Data.SqlClient.SqlDataAdapter(SqlString, CNN)
            AbrirDataAdapter = da
    
        End Function
        Public Function ExecutaSQL(ByVal SqlString As String) As Boolean
            Dim da As Data.SqlClient.SqlCommand
    
            Try
                da = New Data.SqlClient.SqlCommand(SqlString, CNN)
                ExecutaSQL = True
    
            Catch ex As Exception
                ExecutaSQL = False
    
            End Try
    
        End Function
    End Class
    and in the form...
    Code:
     Dim estadoConexao As Boolean    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    
          estadoConexao = db.Conectar()
            MsgBox(db.ExecutaSQL("Insert into IVA values (2, 'aa', 20)"))
        End Sub
    I'm moving from VB6 so I used to do this kind of class using Recordsets...

    Thanks

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Data - SQL class

    You need to assign the connection to the command object. It is never done in you code.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: [2005] Data - SQL class

    Code:
     Public CNN As New Data.SqlClient.SqlConnection
    '...
     Public Function ExecutaSQL(ByVal SqlString As String) As Boolean
            Dim da As Data.SqlClient.SqlCommand
    
            Try
                da = New Data.SqlClient.SqlCommand(SqlString, CNN)
                ExecutaSQL = True
    
            Catch ex As Exception
                ExecutaSQL = False
    
            End Try
    
        End Function
    I think I'm doing that with "CNN", aint I?
    Advice, please

    sorry for any misunderstood

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Data - SQL class

    assuming that this is supposed to run the SQL:
    Code:
        Public Function ExecutaSQL(ByVal SqlString As String) As Boolean
            Dim da As Data.SqlClient.SqlCommand
    
            Try
                da = New Data.SqlClient.SqlCommand(SqlString, CNN)
                ExecutaSQL = True
    
            Catch ex As Exception
                ExecutaSQL = False
    
            End Try
    
        End Function
    I can see why it doesn't "work".... actually, I should say, it does work. It does EXACTLY what you told it to do... which is to create the command object, and it does... BUT.... creating the command doesn't do anything UNTIL you call an execute method .... .ExecureNonQuery, .ExecuteReader, .ExecuteScalar or use it to fill a DataTable.

    In short, you're missing the code to actually execute the SQL.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: [2005] Data - SQL class

    Code:
        Public Function ExecutaSQL(ByVal SqlString As String) As Boolean
            Dim da As Data.SqlClient.SqlCommand
    
            Try
                da = New Data.SqlClient.SqlCommand(SqlString, CNN)
                da.ExecuteNonQuery()
                ExecutaSQL = True
    
            Catch ex As Exception
                ExecutaSQL = False
    
            End Try
    
        End Function
    Thank you!
    Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width