我知道我可以使用SMO找到表行数,但是我在获取视图行数时遇到了麻烦。 0条记录)。
到目前为止,这是我的尝试:
public Int64 GetRowCount()
{
SqlConnection sqlConnection = new SqlConnection(SqlConnectionString);
ServerConnection serverConnection = new ServerConnection(sqlConnection);
Server server = new Server(serverConnection);
if (server == null)
throw new InvalidDataException(
string.Format(
"Could not connect to server {0}. Check that it exists and that the current user has access to it.",
SqlServerName));
Database db = server.Databases[SqlDatabaseName];
if (db == null)
throw new InvalidDataException(
string.Format(
"Could not connect to database {0} on server {1}. Check that it exists and that the current user has access to it.",
SqlDatabaseName, SqlServerName));
db.DefaultSchema = SqlSchemaName;
if (db.Tables.Contains(SqlTableName))
{
Table tbl = db.Tables[SqlTableName];
return tbl.RowCount;
}
if (db.Views.Contains(SqlTableName))
{
View view = db.Views[SqlTableName];
try
{
view.ReCompileReferences();
}
catch
{
// ignored
}
return view.RowCount; ************************** MAGIC GOES HERE
}
throw new InvalidDataException(string.Format( "The SQL table/view {0} does not exist in database {1}, or is not accessible by the current user.",
SqlTableName, SqlDatabaseName));
}
最佳答案
我认为答案是“否”,除非该视图是索引(或物化)视图。我说这是因为SMO似乎正在查看sys.partitions中的元数据来确定行数,除非该视图下的数据持久保存在磁盘上,否则那里将不存在任何行。只有在索引视图时才会发生这种情况。
关于c# - 使用Sql管理对象(SMO)连接到SQL,如何确定 View 是否有行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30670938/