Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Performance enhancement of a vb.net function

Posted on 2011-10-21
5
Medium Priority
?
210 Views
Last Modified: 2012-05-12
Hi,

Is there a better way of writing this function for speed enhancement.I am using visual studio 2005.

Cheers
Public Function CleanSQL(ByVal SQL As String)
        If SQL = String.Empty Then
            Return String.Empty
            Exit Function
        End If
        SQL = SQL.Trim
        If SQL.StartsWith("'") Then Return String.Empty
        If SQL.StartsWith("--") Then Return String.Empty
        If SQL.EndsWith(";") Then SQL = SQL.Remove(SQL.Length - 1, 1)
        If SQL.ToUpper.EndsWith("GO") Then SQL = SQL.Remove(SQL.Length - 2, 2)
        SQL = SQL.Trim
        If SQL = vbNewLine Then Return String.Empty
        If SQL.StartsWith(Chr(13)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(vbNewLine) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(8)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(9)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(10)) Then SQL = SQL.Substring(1)
        Return SQL.Trim
    End Function

Open in new window

0
Comment
Question by:RIAS
  • 3
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37005339
Is it slow? Why do you have to use this function?

You can combine lines such as these

If SQL.StartsWith(Chr(13)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(vbNewLine) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(8)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(9)) Then SQL = SQL.Substring(1)
        If SQL.StartsWith(Chr(10)) Then SQL = SQL.Substring(1)

into


If SQL.StartsWith(Chr(13))  or SQL.StartsWith(vbNewLine) or SQL.StartsWith(Chr(8)) or SQL.StartsWith(Chr(9)) or SQL.StartsWith(Chr(10)) Then
  SQL = SQL.Substring(1)
End If
0
 

Author Comment

by:RIAS
ID: 37005411
Thanks CodeCruiser! It is slow compared to vb6.0.Just thinking are there faster string functions to subsitute:
StartsWith,EndsWith,SQL.Remove(SQL.Length - 1, 1)  
 etc.

Cheers
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 2000 total points
ID: 37005415
You can use Trim

If SQL.ToUpper.EndsWith("GO") Then SQL = SQL.Remove(SQL.Length - 2, 2)

to

If SQL.ToUpper.EndsWith("GO") Then SQL = SQL.Trim("GO")
0
 

Author Closing Comment

by:RIAS
ID: 37005512
Cheers mate!!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37008157
Glad to help :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question