Results 1 to 9 of 9

Thread: How to Query SQL programmatically

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2019
    Posts
    26

    How to Query SQL programmatically

    Hi Guys,

    im trying to create a code that will output result based on 3 [Request Status]. im trying to Query "Pending", "In Progress" and "Completed". below codes work perfectly fine for me but the problem is i have to recreate below codes three times.

    is there a way to code this that will use basically below and output the count of Pending, Completed and In Progress of my table?

    Dim conn As New SqlConnection
    conn.ConnectionString = "Data Source"
    Dim sql As String = ("SELECT Count(*) as TotalNumber FROM [Helpdesk Queries]
    where CONVERT(VARCHAR, [Request Status])='Pending'")
    Dim cmd As New SqlCommand(sql, conn)
    cmd.Connection = conn
    conn.Open()
    Dim Total As Integer
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    Try
    While dr.Read
    Total = dr("TotalNumber")
    MsgBox(Total)
    'Label35.Text = dr("TotalNumber")
    End While
    Finally
    dr.Close()
    conn.Close()
    End Try


    Sorry Guys, Got it to work. below is my working code

    Dim conn As New SqlConnection
    Dim sql As String = ("SELECT COUNT(Id) as TotalNumber, Type
    FROM [Uploadtest]
    GROUP BY Type")
    Dim cmd As New SqlCommand(sql, conn)
    cmd.Connection = conn
    conn.Open()
    Dim maxval As Integer
    Dim typing As String
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    Try
    While dr.Read
    typing = dr("type")
    maxval = dr("TotalNumber")
    If typing = "Completed" Then
    MsgBox(maxval)
    End If
    If typing = "In Progress" Then
    MsgBox(maxval)
    End If
    If typing = "Pending" Then
    MsgBox(maxval)
    End If
    End While
    Finally
    dr.Close()
    conn.Close()
    End Try
    Last edited by ahmish; Nov 25th, 2019 at 06:22 AM.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: How to Query SQL programmatically

    Do you know how to code stored procedures? This seems like a good candidate.
    Please remember next time...elections matter!

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: How to Query SQL programmatically

    or try it this way

    Code:
    SELECT Count(*) as TotalNumber , [Request Status]
    FROM [Helpdesk Queries]
    Group By [Helpdesk Queries].[Request Status], [Helpdesk Queries].[Request Status];
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to Query SQL programmatically

    Please don't post unformatted code.
    Code:
            Dim conn As New SqlConnection
            conn.ConnectionString = "Data Source"
            Dim sql As String = ("SELECT Count(*) as TotalNumber FROM [Helpdesk Queries]
            where CONVERT(VARCHAR, [Request Status])='Pending'")
            Dim cmd As New SqlCommand(sql, conn)
            cmd.Connection = conn
            conn.Open()
            Dim Total As Integer
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            Try
                While dr.Read
                    Total = dr("TotalNumber")
                    MsgBox(Total)
                    'Label35.Text = dr("TotalNumber")
                End While
            Finally
                dr.Close()
                conn.Close()
            End Try
    Code:
            Dim conn As New SqlConnection
            Dim sql As String = ("SELECT COUNT(Id) as TotalNumber, Type 
                                FROM [Uploadtest]
                                GROUP BY Type")
            Dim cmd As New SqlCommand(sql, conn)
                cmd.Connection = conn
                conn.Open()
            Dim maxval As Integer
            Dim typing As String
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            Try
                While dr.Read
                    typing = dr("type")
                    maxval = dr("TotalNumber")
                    If typing = "Completed" Then
                        MsgBox(maxval)
                    End If
                    If typing = "In Progress" Then
                        MsgBox(maxval)
                    End If
                    If typing = "Pending" Then
                        MsgBox(maxval)
                    End If
                End While
            Finally
                dr.Close()
                    conn.Close()
                End Try

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to Query SQL programmatically

    Quote Originally Posted by TysonLPrice View Post
    Do you know how to code stored procedures? This seems like a good candidate.
    I wouldn't have thought so. Of course, any SQL can be programmed into a stored procedure but this is so basic that there seems little specific reason to do so. If you're a database guy and you want all your SQL in your database then fair enough. As an application developer, I say keep all your code in your application if you can. If you need similar code in multiple places then extract out the commonality and put it in a method.

  6. #6
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: How to Query SQL programmatically

    Quote Originally Posted by jmcilhinney View Post
    I wouldn't have thought so. Of course, any SQL can be programmed into a stored procedure but this is so basic that there seems little specific reason to do so. If you're a database guy and you want all your SQL in your database then fair enough. As an application developer, I say keep all your code in your application if you can. If you need similar code in multiple places then extract out the commonality and put it in a method.
    We prefer stored procedures so if we need minor maintenance we don't have to roll code out. In our environment it is much easy to get SQL into production versus binaries. It also lets the DBA group feel like they aren't being left out
    Please remember next time...elections matter!

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

    Re: How to Query SQL programmatically

    It also allows the reuse of query plans much better
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,836

    Re: How to Query SQL programmatically

    Quote Originally Posted by GaryMazzone View Post
    It also allows the reuse of query plans much better
    Good point...

    Another place keeping it in code bit us a little is when we migrated from SQL 2000 to 2008. We had all kinds of applications that had SQL statements in it that we had to make changes to. A big example is how using the "With" keyword changed. We were scanning all kinds of code and rolling out lots of projects. Had all that SQL been in a database it would have been easier.
    Please remember next time...elections matter!

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

    Re: How to Query SQL programmatically

    We have 2 main applications where I work.... One uses in-line SQL (written by Nhibernate) and the other Stored Procs that the DBAs vet. When something goes wrong in the one with in-line SQL we are still expected to figure out what went wrong with the data, not the developers who wrote the thing but someone who has no access to the code. Fun right.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

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