打算写个工具,直接从数据库按字段生成类,使用到了sp_tables,遇到了很奇怪的问题。 查MSDN可以知道:sp_tables的参数之一:@table_type varchar(100) 可选项有: TABLE 、 SYSTEMTABLE 、 VIEW 。 我写了如下代码: Databasedb = DatabaseFactory.CreateDatabase(); DbCommand com = new SqlCommand(); com.CommandText = "sp_tables"; com.CommandType = CommandType.StoredProcedure; com.Parameters.AddWithValue("@table_type", "TABLE"); IDataReader reader = db.ExecuteReader(com); 却拿不到任何东西。不加@talbe_type的话又返回了大量的系统VIEW。 查看sp_tables的源码,在关于@talbe_type的部分发现如下的东西: if (charindex('''TABLE''',@table_type) 0) 倒回去 仔细 看MSDN,参数的说明部分: [ , [ @table_type = ] "'type'" ] 比一般的多了2个",而且下面也有说明 Single quotation marks must enclose each table type, and double quotation marks must enclose the whole parameter. Table types must be uppercase. If SET QUOTED_IDENTIFIER is ON, each single quotation mark must be doubled and the whole parameter must be enclosed in single quotation marks. 太大意了。 修改代码: com.Parameters.AddWithValue(...