如何在 GetSchema 方法的 table 名称中使用通配符?
How use wild card in table name for GetSchema method?
我想使用oledb GetSchema 方法查询数据库中名称以特定字符串开头的所有现有表。
换句话说,我正在寻找等效于以下 sql 查询:
SELECT * FROM information_schema.tables WHERE table_name LIKE 'customer%'
类似于:
String[] tableRestrictions = new String[4];
tableRestrictions[2] = "%customer";
DataTable customerTables = conn.GetSchema("Tables", tableRestrictions );
在这种情况下,我要查询的 table 名称是动态的。因此,我需要先获取整个 table 名称。
似乎没有有效的方法可以做到这一点,只能使用迭代。
我得出的结论是使用 Linq 提供了最简洁的解决方案(感谢 Tim's answer to a similar case):
// Get the tables using GetSchema:
dtbTables = dbConnection.GetSchema("tables");
// use linq to get the table whose name matches the criteria:
DataRow recSpecificTable = dbConnection.GetSchema("Tables").AsEnumerable()
.Where(r => r.Field<string>("TABLE_NAME")
.StartsWith("customer")).FirstOrDefault();
// Now the table name I am looking for:
tableName = Convert.ToString(recSpecificTable["TABLE_NAME"]).Trim();
我想使用oledb GetSchema 方法查询数据库中名称以特定字符串开头的所有现有表。 换句话说,我正在寻找等效于以下 sql 查询:
SELECT * FROM information_schema.tables WHERE table_name LIKE 'customer%'
类似于:
String[] tableRestrictions = new String[4];
tableRestrictions[2] = "%customer";
DataTable customerTables = conn.GetSchema("Tables", tableRestrictions );
在这种情况下,我要查询的 table 名称是动态的。因此,我需要先获取整个 table 名称。
似乎没有有效的方法可以做到这一点,只能使用迭代。 我得出的结论是使用 Linq 提供了最简洁的解决方案(感谢 Tim's answer to a similar case):
// Get the tables using GetSchema:
dtbTables = dbConnection.GetSchema("tables");
// use linq to get the table whose name matches the criteria:
DataRow recSpecificTable = dbConnection.GetSchema("Tables").AsEnumerable()
.Where(r => r.Field<string>("TABLE_NAME")
.StartsWith("customer")).FirstOrDefault();
// Now the table name I am looking for:
tableName = Convert.ToString(recSpecificTable["TABLE_NAME"]).Trim();