Я знаю, что могу найти таблицу Rowcount с помощью SMO, но мне не удается получить представление Rowcount .. Мне действительно не нужна точная строка (это бонус), но мне нужно знать, является ли представление «пустым» "(0 записей).Использование объектов управления Sql (SMO) для подключения к SQL, как узнать, имеет ли представление какие-либо строки?
Вот моя попытка до сих пор:
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));
}
, что имеет большой смысл, поэтому я вынужден сделать запрос, чтобы увидеть, есть ли записи, потому что информация Я хочу, не хранится в метаданных .. – user230910