Results 1 to 19 of 19

Thread: How to handle queries for each form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    How to handle queries for each form

    Hi. I'm using Insert, Update and delete queries on each form, according to the need of form. But is it a right way or I should adapt some other way? Please guide me. I want to improve my development strategies.

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: How to handle queries for each form

    You'll need to elaborate a little bit before we can give you an answer that will be of value to you.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to handle queries for each form

    I mean that all the queries are working on same form, not in a separate module. Like programmers use a module and put all the queries there and just send them parameters from all forms. So which way is gonna helping. please guide me.

  4. #4
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: How to handle queries for each form

    you mean, just like an N tier application
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5

  6. #6
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: How to handle queries for each form

    You can still program it as an N tier application, 1 PC or many.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  7. #7

  8. #8
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: How to handle queries for each form

    multi tier application for example Data Layer, Data Access Class, Entity Class , GUI Class
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  9. #9

  10. #10
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: How to handle queries for each form

    I mean that all the queries are working on same form, not in a separate module. Like programmers use a module and put all the queries there and just send them parameters from all forms. So which way is gonna helping. please guide me
    do you mean to say that you will save the SQL statements in a module and pass on the parameters to the command object of some function / sub routine in a module ?
    are you looking for one such ?
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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

    Re: How to handle queries for each form

    Quote Originally Posted by CoachBarker View Post
    multi tier application for example Data Layer, Data Access Class, Entity Class , GUI Class
    oi.... no it doesn't... that's a multi-layered application... tiering the system means moving things to physically separated areas... such as having a database server, and application server, client, business server and so on... tiers and layers are not the same thing. Although you can't really have tiers w/o layers as a result they often go hand in hand... but you can layer an app, and still have only 1-tier... which is what it sounds like ADQUSIT is looking for. What you can't have is multiple tiers with a single layer... well, you could in theory, but something would have to be on crack in order to make it work.

    ADQUSIT - in the end, it's all about maintainability... can you, six months from now make changes w/o affecting a whole string of code? If you're going to be the only one ever touching it, and you understand it, then go with what you want. If someone else is going to be going into it after you, then look at it form the 6-months ... If you want some kind of advice on how it's typically done in the field... then yes, things are broken out by functionality or some other arbitrary means and put into classes of some kind.

    -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??? *

  12. #12
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: How to handle queries for each form

    tech
    i would like to take some of your time here
    i am not a professional programmer and am not in that field , i am a indian railways train driver
    how exactly the IT companies doe's,
    i mean for example, if i am developing a desk top application and a professional developer developing
    which way it makes a difference. ( just out of curiosity ), i believe you are professional !

    why the software piece costs so much costly, is this

    maintainability
    quality of code
    supply of source code
    annual maintenance contract
    application of mind

    what it is ?
    Last edited by make me rain; Apr 15th, 2013 at 11:58 AM. Reason: spell mistake
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

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

    Re: How to handle queries for each form

    Why?
    Short answer: there's a lot of overhead... marketing, packaging, talent, taxes, etc... all that stuff adds up.

    The longer answer is that to do things right, it takes time. Pure and simple. And it takes talent and knowledge. Neither comes cheap. It's the same reason people pay others to do their taxes for them. They (hopefully) have the knowledge to fill out all the forms right. I am good at what I do, and as a result I can charge a premium for my time. Just about anyone can crank out code. Cranking out the right kind of code is another matter.

    there's a number of things that determine the price of a piece of software... it's value plays a big part in that... if I create something no one's seen, but everyone wants it... I can charge quite a bit for it... if there's competitors, then I'd better be doing something better, or (most likely "and" rather than "or") I need to lower the price to be competitive... free market... supply & demand...


    -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??? *

  14. #14
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: How to handle queries for each form

    My apologies read it wrong.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: How to handle queries for each form

    Quote Originally Posted by CoachBarker View Post
    My apologies read it wrong.
    Pffft! I wouldn't worry about it... it's a common misconception... heck for years I operated under the idea that they were the same... finally took someone to step me through it before I really understood the differences... and of course the pedantic nature of me (and quite possibly the CDO side of me) wants to educate people when I see the terms being misapplied.

    -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??? *

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to handle queries for each form

    do you mean to say that you will save the SQL statements in a module and pass on the parameters to the command object of some function / sub routine in a module ?
    are you looking for one such ?
    yes its a way, about which i want to ask opinion, that which way is better to use?

    @ TechGnome
    I didn't understand it:
    ADQUSIT - in the end, it's all about maintainability... can you, six months from now make changes w/o affecting a whole string of code? If you're going to be the only one ever touching it, and you understand it, then go with what you want. If someone else is going to be going into it after you, then look at it form the 6-months

  17. #17
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: How to handle queries for each form

    my straight answer is don't do it
    because it serves no purpose because,
    the SQL statement which you passes on to the command object includes the parameters often, and which must be in the same routine on the command,connection,adapter etc resides.

    if you store it in other place alone , & as the application grows the confusion creates the havoc
    secondly the SQL statements are 99 % unique ( for example inserting,editing,deleting into different tables )

    How ever you can build a class which can build a SQL string and then execute the statement , of course it saves some lines of code and development time but it's nasty ( i don't know about the security concerns )

    vb.net Code:
    1. Public Class Generals
    2.  
    3.     Public Sub SaveMethod(ByVal TableName As String, _
    4.                           ByVal FieldNames() As String, _
    5.                           ByVal Params() As String, _
    6.                           ByVal Field_Values() As String, ByVal Cnn As MySqlConnection)
    7.  
    8.         '' saves the new values
    9.  
    10.         Dim Str_Sql As New StringBuilder
    11.         With Str_Sql
    12.             .Append("INSERT INTO ")
    13.             .Append(TableName)
    14.             .Append("( ")
    15.         End With
    16.  
    17.         '' append field names
    18.         For Each Sx As String In FieldNames
    19.             With Str_Sql
    20.                 .Append(Sx.ToString & ",")
    21.             End With
    22.         Next
    23.  
    24.         '' remove extra coma
    25.         With Str_Sql
    26.             .Append(".").Replace(",.", ")")
    27.             .Append(" VALUES ( ")
    28.         End With
    29.  
    30.         '' add params
    31.         For Each Rx As String In Params
    32.             With Str_Sql
    33.                 .Append(Rx.ToString & ",")
    34.             End With
    35.         Next
    36.  
    37.         '' Remove extra coma
    38.         With Str_Sql
    39.             .Append(".").Replace(",.", ")")
    40.             .Append(" ;")
    41.         End With
    42.  
    43.         Dim SQL_save As String = Str_Sql.ToString
    44.         Dim Cmd_Insert As New MySqlCommand(SQL_save, Cnn)
    45.         Dim Connection As New Connector
    46.  
    47.         '' Add parameters
    48.         Dim I As Byte = 0
    49.         For Each FV As String In Field_Values
    50.  
    51.             With Cmd_Insert
    52.                 .Parameters.AddWithValue(Params(I).ToString, FV)
    53.             End With
    54.  
    55.             I += 1
    56.         Next
    57.  
    58.         With Cmd_Insert
    59.  
    60.             Connection.Cobs_Connect(True, Cnn)
    61.             .ExecuteNonQuery()
    62.             Connection.Cobs_Connect(False, Cnn)
    63.  
    64.             .Dispose()
    65.         End With
    66.  
    67.         TableName = Nothing
    68.         FieldNames = Nothing
    69.         Params = Nothing
    70.         Field_Values = Nothing
    71.  
    72.     End Sub
    73.  
    74.     Public Sub EditMethod(ByVal TableName As String, _
    75.                           ByVal FieldNames() As String, _
    76.                           ByVal Params() As String, _
    77.                           ByVal Field_Values() As Object, _
    78.                           ByVal KeyField As String, _
    79.                           ByVal KeyParam As String, ByVal KeyValue As String, _
    80.                           ByVal Cnn As MySqlConnection)
    81.  
    82.         '' saves the new values
    83.  
    84.         Dim Str_Sql As New StringBuilder
    85.  
    86.         With Str_Sql
    87.             .Append("UPDATE ")
    88.             .Append(TableName)
    89.             .Append(" SET ")
    90.         End With
    91.  
    92.         Dim iX As Byte = 0
    93.         For Each sX As String In FieldNames
    94.  
    95.             With Str_Sql
    96.                 .Append(String.Concat(sX, " = ", Params(iX).ToString, ","))
    97.             End With
    98.  
    99.             iX += 1
    100.         Next
    101.  
    102.         '' remove extra coma
    103.         With Str_Sql
    104.             .Append(".").Replace(",.", "")
    105.         End With
    106.  
    107.         With Str_Sql
    108.             .Append(" WHERE ")
    109.             .Append(String.Concat(KeyField, " = ", KeyParam, " ;"))
    110.         End With
    111.  
    112.  
    113.         Dim Cmd_Edit As New MySqlCommand(Str_Sql.ToString, Cnn)
    114.         Dim Connection As New Connector
    115.  
    116.         '' Add parameters
    117.         Dim I As Byte = 0
    118.         For Each FV As Object In Field_Values
    119.  
    120.             With Cmd_Edit
    121.                 .Parameters.AddWithValue(Params(I).ToString, FV)
    122.             End With
    123.  
    124.             I += 1
    125.         Next
    126.  
    127.         '' add where clause
    128.  
    129.         With Cmd_Edit
    130.             .Parameters.AddWithValue(KeyParam, KeyValue)
    131.         End With
    132.  
    133.         With Cmd_Edit
    134.  
    135.             Connection.Cobs_Connect(True, Cnn)
    136.             .ExecuteNonQuery()
    137.             Connection.Cobs_Connect(False, Cnn)
    138.  
    139.             .Dispose()
    140.         End With
    141.  
    142.         TableName = Nothing
    143.         FieldNames = Nothing
    144.         Params = Nothing
    145.         Field_Values = Nothing
    146.  
    147.     End Sub
    148.     Public Sub DeleteMethod(ByVal TableName As String, _
    149.                           ByVal FieldNames As String, _
    150.                           ByVal Params As String, _
    151.                           ByVal Field_Values As String, ByVal Cnn As MySqlConnection)
    152.  
    153.         '' saves the new values
    154.  
    155.         Dim Str_Sql As New StringBuilder
    156.  
    157.         With Str_Sql
    158.             .Append(" DELETE FROM ")
    159.             .Append(TableName)
    160.             .Append(" WHERE ")
    161.             .Append(String.Concat(TableName, ".", FieldNames, " = ", Params, " ;"))
    162.         End With
    163.  
    164.         Dim S As String = Str_Sql.ToString
    165.  
    166.         Dim Cmd_Drop As New MySqlCommand(S, Cnn)
    167.         Dim Connection As New Connector
    168.  
    169.         '' Add parameters
    170.         With Cmd_Drop
    171.             .Parameters.AddWithValue(Params, Field_Values)
    172.         End With
    173.  
    174.         With Cmd_Drop
    175.  
    176.             Connection.Cobs_Connect(True, Cnn)
    177.             .ExecuteNonQuery()
    178.             Connection.Cobs_Connect(False, Cnn)
    179.  
    180.             .Dispose()
    181.         End With
    182.  
    183.         TableName = Nothing
    184.         FieldNames = Nothing
    185.         Params = Nothing
    186.         Field_Values = Nothing
    187.  
    188.     End Sub

    now to use the calss
    vb.net Code:
    1. Dim Gen As New Generals
    2.         Dim Cnn29 As New MySqlConnection
    3.  
    4.         Dim FieldNames() As String = {"cugno", "perno", "pmedue", "grs_due", "tech_due", "nliid"}
    5.         Dim Params() As String = {"@cugno", "@perno", "@pmedue", "@grs_due", "@tech_due", "@nliid"}
    6.         Dim FieldValues() As Object = {CUGnumber, PersNumber, PmeDue, GrsDue, TechDue, Liid}
    7.  
    8.         Gen.EditMethod("crewdetail", FieldNames, Params, FieldValues, "Detailid", "@Detailid", detailId, Cnn29)
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to handle queries for each form

    I'm using parametrized queries on my forms and have all common objects like sqlconnection, dataAdapter and DataSet.

    What i want now is that i pass the parameters to module, where all the process on query is done and just give me the end result on my form.

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to handle queries for each form

    This is my module code:

    This is my connection code:
    Code:
     Public Function Conn()
            Try
                If cnSql.State = ConnectionState.Closed Then
                    cnSql.Open()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
            Return cnSql
        End Function
    This is my insert sub routine:
    Code:
    #Region "Insertion"
    
        Public Sub Insertion(ByVal tblName As String, ByVal columns As String, ByVal Parameters As String)
    
            Try
                cmdsql1.CommandText = "insert into " & tblName & " ( " & columns & ")  values  ( " & Parameters & ")"
                cmdsql1.Connection = Conn()
                cmdsql1.ExecuteNonQuery()
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
    #End Region
    And this is my save button code on my main form:

    Code:
    Try
                Insertion("ProductBasicInfo", "ProdId, ProdName, Description, Manufacturer", " " & txtProdID.Text.Trim & ", '" & txtProdName.Text.Trim & "', '" & txtProdDesc.Text.Trim & "', '" & txtProdManuf.Text.Trim & "'")
                MessageBox.Show("Record Inserted Successfully")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    From my form, i pass the parameters to module, but i'm not satisfied with this way of code, because this query may have sql injection.

    Please guide me that how to use parametrized query in module level. Though I always deal queries on single form in parametrized way, but can't use on module level.

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