Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: , +
I want to put my SQL commands into a Module, so that whenever I have a command to execute, I can set up a couple of variables and call the Module, therefore avoiding repeat code and streamlining my logic. Something like :

VB
mySqlExpression = "DELETE FROM Leagues Where LeagueName = '" & myLeague & "'"
            mySqlToReturn = "N" 'No return is needed
            myVbExp = ""
            Call RunSQL(mySqlExpression, mySqlToReturn, myVbExp)


VB
Module SQLCmd
    Public Function RunSQL(mySqlLine As String, myRetOpts As String, myVbStr As String)
        mySqlCmd = New SqlCeCommand(mySqlLine, mySqlConn)
        If mySqlConn.State = ConnectionState.Closed Then mySqlConn.Open()
        Dim mySqlRdr As SqlCeDataReader = mySqlCmd.ExecuteReader()
        mySqlCmd.ExecuteNonQuery()
        If myRetOpts = "N" Then 
            'do nothing
        Else
            '??? Extra commands here
        End If
        mySqlConn.Close()
    End Function
End Module


Is this a good idea / an unusual idea / standard practice ?!? I think it seems fine for simple commands (and in this particular project of mine nothing too complicated is going to be done) but I am having trouble when it comes to passing commands & values back & forth if I allow it to be generic.

What I mean is, I can create one or more modules to accept no extra commands or 1 exactly, 2 exactly, etc, but I'm not sure how I would go about allowing a variable number of parameters to be passed & processed.

Any thoughts ?
Posted

VB
dim sqlcmd as new sqlclient.sqlcommand( strCommand, strConnection )

dim param as sqlclient.sqlparameter

param = new sqlClient.sqlparameter( "@Param", int )
param.value = value
sqlcmd.parameters.add( param )


Look at stored procedures and SQL parameters
 
Share this answer
 
It is kind of a bad idea to put your main SQL code in a module: if your database requirements change you will need to recompile and redistribute your app, which can become a major headache. You are better off using views and stored procedures on the SQL server itself, especially for core components that will get called repeatedly.

If you do chose to go with using app-side SQL, USE PARAMETERIZED QUERIES. A piece of code like
VB
"DELETE FROM Leagues Where LeagueName = '" & myLeague & "'"

will get compiled and stored separately for EACH different myLeague. This ends up taking a lot of cycles: time to sort through the dozens of other DELETE FROM Leagues with different names, time to compile yet another variation, time to store that new version in memory in case it gets used again, and time to clear out the compiled cache when it gets full.

Instead, use
VB
"DELETE FROM Leagues Where LeagueName = @LeagueName"

and pass your parameter in with a SqlParameter object. Your SQL Server will have only one variant to compile, and will use that one variant over and over again.
 
Share this answer
 
Comments
Gary Heath 20-Jul-12 10:35am    
I am using SQL-CE at the moment, as this is a very small 1-user application. Is this still capable of using "views and stored procedures" ?
Gregory Gadow 20-Jul-12 10:52am    
Oops, my bad: I didn't notice that you had this flagged as SQL-CE. That version of SQL does not support programmability, so ignore what I wrote above. Given that, I would do what you already thought of and put each query and command in a separate function. Having only one instance where you are deleting a league will make sure that updates to your code are consistent (only one place to edit the delete command, rather than a dozen.)

I would recommend functions so that you can pass a result value back to the caller. Commands like delete or insert could return a boolean value indicating success, and queries could return a table object with your results. SQL-CE does support parameters, so I would still use those rather than hard-coding your values: it is a good habit to get into, and will probably have an efficiency benefit similar to what you see in a full feature SQL server.
Gary Heath 20-Jul-12 11:12am    
Thank you, I'm doing a bit of research into all this now. This is a learning exercise for me more than anything (though my user will still use it in his production environment) and I think that when I progress onto the main project I am supposed to do for him, then I will be using *real* SQL-Server, as it's for a multi-user web environment, so I'll definitely remember your comments when the time comes ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900