使用 Sql 管理对象 (SMO) 连接到 SQL,如何确定视图是否有任何行?
Using Sql Management Objects (SMO) to connect to SQL, how can I find out if a view has any rows?
我知道我可以使用 SMO 找到 Table 行数,但是我在获取视图行数时遇到了问题。我真的不需要确切的行数(这是一个额外的好处),但我确实需要知道视图是否为 "empty"(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));
}
如果找不到其他方法,可以随时使用ExecuteWithResults 方法select 单行单字段。然后您可以检查结果以查看它是否返回了任何行。
var server = new Server();
var db = server.Databases("northwind");
var results = db.ExecuteWithResults("select top 1 ID from [ViewName]");
只需使用Ado.Net连接执行命令:
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = string.Format(@"if exists(select * from {0})
select 1 as exist
else
select 0 as exist", SqlTableName);
var result = (int)cmd.ExecuteScalar();
如果结果 == 1 则存在一些记录
我认为答案是 "no",除非视图是索引(或物化)视图。我之所以这么说,是因为 SMO 似乎正在查看 sys.partitions 中的元数据以确定行数,除非视图下的数据持久保存到磁盘,否则那里不会存在任何行。只有当视图被索引时才会发生这种情况。
我知道我可以使用 SMO 找到 Table 行数,但是我在获取视图行数时遇到了问题。我真的不需要确切的行数(这是一个额外的好处),但我确实需要知道视图是否为 "empty"(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));
}
如果找不到其他方法,可以随时使用ExecuteWithResults 方法select 单行单字段。然后您可以检查结果以查看它是否返回了任何行。
var server = new Server();
var db = server.Databases("northwind");
var results = db.ExecuteWithResults("select top 1 ID from [ViewName]");
只需使用Ado.Net连接执行命令:
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = string.Format(@"if exists(select * from {0})
select 1 as exist
else
select 0 as exist", SqlTableName);
var result = (int)cmd.ExecuteScalar();
如果结果 == 1 则存在一些记录
我认为答案是 "no",除非视图是索引(或物化)视图。我之所以这么说,是因为 SMO 似乎正在查看 sys.partitions 中的元数据以确定行数,除非视图下的数据持久保存到磁盘,否则那里不会存在任何行。只有当视图被索引时才会发生这种情况。