C语言中使用OdbcDataAdapter的表名格式问题
Let's work through the issue you're facing step by step:
1. Fix the Table Name Discrepancy First
I noticed a key detail in your description: the working Excel query uses kzn-57 | 600 | Survey Disabled | Realtime, but your target table is kz-50 | 600 | Data Disabled | Realtime. Even a tiny difference like kzn vs kz will trigger the "Unknown Table Name" error. Double-check you're using the exact, correct table name in your C# code—copy-paste it directly if possible to avoid typos.
2. Handle Special Characters in the Table Name
Your table name has spaces, hyphens, and pipes, which are all special characters for SQL identifiers. ODBC drivers can vary in how they handle these, so try these formatting approaches:
Option A: Escape Double Quotes Correctly in C#
In C#, you need to properly escape double quotes inside strings. You can either use backslashes or verbatim strings (prefixed with @) for readability:
// Using escaped double quotes string queryString = "SELECT * FROM ADI.\"kz-50 | 600 | Data Disabled | Realtime\""; // Using verbatim string (cleaner for complex names) string queryString = @"SELECT * FROM ADI.""kz-50 | 600 | Data Disabled | Realtime""";
This matches the format that worked in Excel, so it's the first thing to test.
Option B: Try Square Brackets (Driver-Dependent)
Some ODBC drivers prefer square brackets to wrap identifiers with special characters. Modify your query like this:
string queryString = "SELECT * FROM ADI.[kz-50 | 600 | Data Disabled | Realtime]";
Option C: Use Backticks (If Your Database Supports It)
If your custom database uses syntax similar to MySQL or PostgreSQL, backticks might be the right escape character:
string queryString = "SELECT * FROM ADI.`kz-50 | 600 | Data Disabled | Realtime`";
3. Match the ODBC Driver Excel Uses
Make sure the ODBC driver in your C# app is identical to the one Excel uses. Different drivers have different rules for identifier quoting and table resolution. Check Excel's connection settings to find the driver name, then use that exact driver in your C# ODBC connection string.
4. Verify the Table Exists in the ADI Schema
To confirm the exact table name stored in the database, run a query to list all tables in the ADI schema. If your database supports standard INFORMATION_SCHEMA, use this:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'ADI';
Copy the table name directly from the results into your query to eliminate any hidden formatting issues.
内容的提问来源于stack exchange,提问作者michal

