Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:32:46+00:00 2026-05-25T22:32:46+00:00

I’ve got a not particularly complex SQL query that returns exactly what I want

  • 0

I’ve got a not particularly complex SQL query that returns exactly what I want when run in Server Mgt Studio, but when I run it in C# the DataGrid comes up blank. (And yes – hostnames sanitized, I don’t just have a terrible naming scheme.)

Hopefully someone with sharper eyes than I can pick up the issue.

SQL query:

SELECT MachineName, InstanceName, CounterName, 
AVG(CASE WHEN CounterValue > 0 THENCounterValue ELSE NULL END)/1024 as 'Mean (KB)'
FROM DataCollector.dbo.JobCounterSummary
WHERE 
((MachineName = '\\HOST001' AND (InstanceName = 'D:' OR InstanceName = 'E:')) OR
(MachineName = '\\HOST002' AND (InstanceName = 'E:' OR InstanceName = 'G:')) OR
(MachineName = '\\HOST003' AND (InstanceName = 'G:' OR InstanceName = 'H:')) OR 
(MachineName = '\\HOST004' AND (InstanceName = 'G:' OR InstanceName = 'H:') OR
(MachineName = '\\HOST005' AND InstanceName = 'G:') OR 
(MachineName = '\\HOST006' AND InstanceName = 'C:') OR 
(MachineName = '\\HOST007' AND InstanceName = 'C:'))) AND
InstanceName != '_Total' AND 
InstanceName NOT LIKE 'Harddisk%' AND 
(CounterName = 'Avg. Disk Bytes/Write' OR CounterName = 'Avg. Disk Bytes/Read') 
GROUP BY MachineName, CounterName, InstanceName 
ORDER BY MachineName, InstanceName, CounterName

And the C# code:

public DataTable dtJobReadWrite = new DataTable();
    private void GetReadWriteStats()
    {
        dtJobReadWrite.Clear();
        string connstr = @"Server=SQLSERVER\SQL;Database=DataCollector;Trusted_Connection=True; MultipleActiveResultSets=true";
        SqlConnection conn = new SqlConnection(connstr);
        conn.Open();
        string commandstr =
            "SELECT MachineName, InstanceName, CounterName, AVG(CASE WHEN CounterValue > 0 THEN CounterValue ELSE NULL END)/1024 as \"Mean (KB)\" " +
            "FROM dbo.JobCounterSummary " +
            "WHERE " +
                "((MachineName = @HOST001 AND (InstanceName = @DriveD OR InstanceName = @DriveE)) OR " +
                "(MachineName = @HOST002 AND (InstanceName = @DriveE OR InstanceName = @DriveG)) OR " +
                "(MachineName = @HOST003 AND (InstanceName = @DriveG OR InstanceName = @DriveH)) OR " +
                "(MachineName = @HOST004 AND (InstanceName = @DriveG OR InstanceName = @DriveH) OR " +
                "(MachineName = @HOST005 AND InstanceName = @DriveG) OR " +
                "(MachineName = @HOST006  AND InstanceName = @DriveC) OR " +
                "(MachineName = @HOST007 AND InstanceName = @DriveC))) AND " +
                "InstanceName != @_Total AND " +
                "InstanceName NOT LIKE @HardDisk AND " +
                "(CounterName = @AvgDiskBytesWrite OR CounterName = @AvgDiskBytesRead) " +
            "GROUP BY MachineName, CounterName, InstanceName " +
            "ORDER BY MachineName, InstanceName, CounterName";

        SqlCommand jobsCommand = new SqlCommand(commandstr, conn);
        jobsCommand.Parameters.AddWithValue("@HOST001", "\\HOST001");
        jobsCommand.Parameters.AddWithValue("@HOST002", "\\HOST002");
        jobsCommand.Parameters.AddWithValue("@HOST003", "\\HOST003");
        jobsCommand.Parameters.AddWithValue("@HOST004", "\\HOST004");
        jobsCommand.Parameters.AddWithValue("@HOST005", "\\HOST005");
        jobsCommand.Parameters.AddWithValue("@HOST006", "\\HOST006");
        jobsCommand.Parameters.AddWithValue("@HOST007", "\\HOST007");
        jobsCommand.Parameters.AddWithValue("@DriveC", "C:");
        jobsCommand.Parameters.AddWithValue("@DriveD", "D:");
        jobsCommand.Parameters.AddWithValue("@DriveE", "E:");
        jobsCommand.Parameters.AddWithValue("@DriveG", "G:");
        jobsCommand.Parameters.AddWithValue("@DriveH", "H:");
        jobsCommand.Parameters.AddWithValue("@_Total", "_Total");
        jobsCommand.Parameters.AddWithValue("@HardDisk", "Harddisk%");
        jobsCommand.Parameters.AddWithValue("@AvgDiskBytesWrite", "Avg. Disk Bytes/Write");
        jobsCommand.Parameters.AddWithValue("@AvgDiskBytesRead", "Avg. Disk Bytes/Read");
        SqlDataAdapter jobsAdapter = new SqlDataAdapter(jobsCommand);
        jobsAdapter.Fill(dtJobReadWrite);
        dgvReadWrites.DataSource = dtJobReadWrite;
        conn.Close();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:32:47+00:00Added an answer on May 25, 2026 at 10:32 pm

    My guess is that it’s your strings:

    "\\HOST001"
    

    You’re not escaping out your backslashes, so the strings only contain a single backslash. Fix it by either:

    "\\\\HOST001"
    //or
    @"\\HOST001"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.