Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

c# - Use SqlConnection.GetSchema to get Tables Only (No Views)

When I use

SqlConnection.GetSchema("Tables");

it returns all the tables AND views for the target database.

Is there any way to just return tables? All the research I have done indicates I am doing this correctly yet it always returns the views like they are tables. I have dug down into the DataTable in debug and I can't even find a difference. The data types are reported the same... As far as I can tell, it can't differentiate between a view and a table. (It does make sense in a way since a view for all intents and purposes IS a table.)

I am using the Northwind database for testing.

I am writing the application in C#.

Here is the code that I am running to get the schema info. Pretty simple.

SQLCon.Open();
DataTable tables = SQLCon.GetSchema("Tables");
SQLCon.Close();

I would very much like to use the getschema method and not have to query the database in another fashion... if at all possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to this article, the returned data table has a column table_type, which tells you whether it's a VIEW or a BASE TABLE.
Use that column to filter out the views on your C# end.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...