How to check if my SQL Server Express database exceeds the 10 GB size limit?

后端 未结 5 1769
感动是毒
感动是毒 2021-02-06 14:38

I am developing a web site, it uses SQL Server 2008 R2 Express for its database. And in testing, there is a lot of data and images stored into this database.

According t

5条回答
  •  误落风尘
    2021-02-06 15:41

    I am using the following method to calculate database current size crucial for comparing with sql size limitations:

        public static int GetDbSizeInMB([NotNull] string connectionString) {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString)) {
                sqlConnection.Open();
                using (var sqlCommand = new SqlCommand()) {
                    sqlCommand.CommandType = CommandType.Text;
                    sqlCommand.CommandText = @"
                        SELECT SUM(CAST(FILEPROPERTY([name],'SpaceUsed') AS int)/128.0) AS [UsedSpaceInMB]
                        FROM sys.database_files
                        WHERE type_desc like 'ROWS' or type_desc like 'FULLTEXT'
                        ";
                    sqlCommand.Connection = sqlConnection;
                    return Convert.ToInt32(sqlCommand.ExecuteScalar());
                }
            }
    

    )

提交回复
热议问题