Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Better the code

  1. #1
    New Coder
    Join Date
    Apr 2010
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Better the code

    Hi to All,

    I' d like to know if t's possible to better the following code:

    Set cn = Server.CreateObject("ADODB.Connection")
    Set cn_cu = Server.CreateObject("ADODB.Connection")
    cn.ConnectionString = "DSN=4_plan.dsn"
    cn_cu.ConnectionString = "DSN=3_custom.dsn"
    cn.Open
    cn_cu.Open

    Set cmd=CreateObject("adodb.command")
    Set cmd_cu=CreateObject("adodb.command")
    With cmd
    Set .ActiveConnection = cn
    .CommandType=1 'adCmdText
    .CommandText = sql3
    .Execute , , 128 '128=adExecuteNoRecords
    .CommandText = sql4
    .Execute , , 128
    End With
    With cmd_cu
    Set .ActiveConnection = cn_cu
    .CommandType=1 'adCmdText
    .CommandText = sql5
    .Execute , , 128
    End With
    '
    cn.Close
    cn_cu.Close
    Set cn=nothing
    Set cn_cu=nothing

    Thanks a lot in advance !!!

    P.S. I' d like to do something like this:

    cn.Close cn_cu.Close
    Set cn=nothing Set cn_cu=nothing

    is it possible ??

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    You can do
    Code:
    cn.Close : cn_cu.Close
    Set cn=nothing : Set cn_cu=nothing
    (Note the : colons in there)

    But that saves you nothing at all. A RETURN character eats no code once the code is compiled and running.

    On the other hand, you can simplify the heck out of the rest of that:
    Code:
    Set cn = Server.CreateObject("ADODB.Connection")
    cn.Open "DSN=4_plan.dsn"
    cn.Execute sql3
    cn.Execute sql4
    cn.Close
    cn.Open  "DSN=3_custom.dsn"
    cn.Execute sql5
    cn.Close
    And there's no need to set cn to Nothing. Waste of coding.

    The really ugly part of the code, there, is the use of DSNs. DSN connections are older, slower, and more error-prone than DSN-less connections.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Old Pedant,

    Thanks a lot for your precious instructions !!!!

    I' ll use them even in future


  •  

    Posting Permissions

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