SQL查询有效,但等效的C#查询没有。 。

时间:2011-10-03 08:09:32

标签: c# sql parameterized

我有一个不是特别复杂的SQL查询,它可以在Server Mgt Studio中运行时返回我想要的内容,但是当我在C#中运行它时,DataGrid会显示为空白。 (是的 - 主机名已经消毒,我不仅仅有一个糟糕的命名方案。)

希望眼睛比我更敏锐的人能够解决这个问题。

SQL查询:

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

和C#代码:

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();
    }

2 个答案:

答案 0 :(得分:5)

我的猜测是你的字符串:

"\\HOST001"

你没有逃避反斜杠,因此字符串只包含一个反斜杠。通过以下任一方法修复:

"\\\\HOST001"
//or
@"\\HOST001"

答案 1 :(得分:0)

as \"Mean (KB)\"

我猜那些应该是单个qoutes ...
在这种情况下,你可能也不必逃避它们。