CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Posts
    95

    How do I display MSSQL Count?

    Code:
    Imports System.Data.SqlClient
    
    Dim SQLStr, ConnString As String
    
    ConnString = "Data Source=Server;Initial Catalog=CDAD;Integrated Security=True"
            Dim SQLConn As New SqlConnection() 'The SQL Connection
            Dim SQLCmd As New SqlCommand() 'The SQL Command
            Dim SQLdr As SqlDataReader        'The Local Data Store 
            SQLConn.ConnectionString = ConnString 'Set the Connection String
    
            SQLStr = "SELECT COUNT(*) FROM TABLE WHERE strComputerName = 'TomsCPU' AND strApplication = 'TomsApplication'" 
            SQLConn.Open() 'Open the connection 
            SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
            SQLCmd.CommandText = SQLStr 'Sets the SQL String
            SQLdr = SQLCmd.ExecuteReader 'Gets Data
    Table Looks Like
    TABLE
    strComputerName

    TomsCPU
    GarysCPU
    TomsCPU

    strOffice
    Florida
    Texas
    FLorida

    strApplication
    TomsApplication
    ExcelX
    TomsApplication
    Now the count should return 2. How do I retrieve the count from the data reader.
    Using VB .Net 2008 Express Edition

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How do I display MSSQL Count?

    Something like this.
    Code:
    While SQLdr.Read
       MessageBox.Show(SQLdr[0].ToString()
    End While

  3. #3
    Join Date
    Mar 2003
    Posts
    95

    Re: How do I display MSSQL Count?

    Thanks for your reply. I was able to figure it out a different way, it looks like the problem was i was using the datareader.

    Code:
    Dim tmpCount As Integer
    tmpCount = SQLCmd.ExecuteScalar 'Gets Data 
    MsgBox(tmpCount)
    Did the trick!
    Using VB .Net 2008 Express Edition

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How do I display MSSQL Count?

    Exactly. ExecuteScalar is the write way of doing it.

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

Featured