缩小数据库日志的工具源码

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

这几天做测试工作,事务很多,日志很大硬盘很快没空间了,把缩小数据库日志的存储过程封装成一个小工具;这个是压缩日志的代码;觉得手工处理日志麻烦的可以考虑用这个工具试试.

None.gif using System;
None.gif using System.Data;
None.gif using System.Data.SqlClient;
None.gif
None.gif namespace WebTruncateLog
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// DataAccess 的摘要说明。
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic class DataAccess
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif私有变量#region 私有变量
InBlock.gifprivate const string sqlMasterDatabase = "master";
InBlock.gifprivate string sqlServerName = "localhost";
InBlock.gifprivate string sqlUserId = "sa";
InBlock.gifprivate string sqlUserPassword = "";
InBlock.gifprivate string sqlDefaultDatabase = "master";
InBlock.gifprivate bool active = false;
ExpandedSubBlockEnd.gif#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif组件对象#region 组件对象
InBlock.gifprivate SqlConnection sqlConn ;
ExpandedSubBlockEnd.gif#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif构造#region 构造
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 构造函数
ExpandedSubBlockEnd.gif
/// </summary>

InBlock.gifpublic DataAccess()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn = new SqlConnection();
InBlock.gif active = false;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif方法#region 方法
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 连接数据库
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="ConnectionString"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool Connect(string ConnectionString)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn.ConnectionString = ConnectionString;
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn.Open();
InBlock.gifthis.active = true;
InBlock.gifreturn true;
ExpandedSubBlockEnd.gif }

InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.active = false;
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 连接数据库
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool Connect()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn.ConnectionString = this.SqlConnectionString;
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn.Open();
InBlock.gifthis.active = true;
InBlock.gifreturn true;
ExpandedSubBlockEnd.gif }

InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.active = false;
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 关闭连接
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool Close()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(sqlConn.State!=System.Data.ConnectionState.Closed)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlConn.Close();
InBlock.gifthis.active = !this.active;
InBlock.gifreturn true;
ExpandedSubBlockEnd.gif }

InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gifelse return false;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 执行无记录返回sql
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="strSQL"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool ExecuteSQL(string strSQL)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(!this.active) return false;
InBlock.gif
InBlock.gif SqlCommand sqlCmd = new SqlCommand(strSQL,sqlConn);
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlCmd.ExecuteNonQuery();
InBlock.gifreturn true;
ExpandedSubBlockEnd.gif }

InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 执行有记录返回sql
InBlock.gif
/// </summary>
InBlock.gif
/// <param name="strSQL"></param>
InBlock.gif
/// <param name="TableName"></param>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic DataTable ExecuteSQL(string strSQL,string TableName)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(!this.active) return null;
InBlock.gif
InBlock.gif System.Data.SqlClient.SqlDataAdapter sqlDa = new SqlDataAdapter(strSQL,sqlConn);
InBlock.gif DataSet ds = new DataSet();
InBlock.giftry
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif sqlDa.Fill( ds,TableName );
InBlock.gifreturn ds.Tables[TableName];
ExpandedSubBlockEnd.gif }

InBlock.gifcatch
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn null;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 切换到master库
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool UseMaster()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.ExecuteSQL("use master");
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 切换到当前库
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool UseDefaultDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.ExecuteSQL("use " + this.sqlDefaultDatabase);
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 获取当前所有的数据库
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic DataTable GetDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.UseMaster(); //切换到master库
InBlock.gif
string sql = "";
InBlock.gif sql += "select\n";
InBlock.gif sql += "[name] as 数据库名称, \n";
InBlock.gif sql += "[dbid] as 数据库ID, \n";
InBlock.gif sql += "[crdate] as 创建日期,\n";
InBlock.gif sql += "[cmptlevel] as 兼容级别,\n ";
InBlock.gif sql += "[filename] as 主文件路径,\n";
InBlock.gif sql += "[version] as 内部版本号\n";
InBlock.gif sql += "from sysdatabases\n";
InBlock.gif sql += "order by dbId asc\n";
InBlock.gif
InBlock.gifreturn ExecuteSQL(sql,"sysdatabases");
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 获取当前库的物理文件
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic DataTable GetSysFile()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.UseDefaultDatabase(); //切换到当前库库
InBlock.gif
string sql = "";
InBlock.gif sql += "select\n";
InBlock.gif sql += "[fileid] as 文件标识号,\n";
InBlock.gif sql += "Cast (([size]*8/1024) as Varchar) + ' 兆' as 文件大小,\n";
InBlock.gif sql += "[name] as 逻辑名, \n";
InBlock.gif sql += "[filename] as 物理名\n";
InBlock.gif sql += "from sysfiles\n";
InBlock.gifreturn ExecuteSQL(sql,"sysfiles");
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//// <summary>
InBlock.gif
/// 执行压缩日志功能
InBlock.gif
/// </summary>
ExpandedSubBlockEnd.gif
/// <returns></returns>

InBlock.gifpublic bool ExecuteTruncateLog(int NewLogFileSize)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifstring LogFile = "";
InBlock.gifstring strDelProc =
InBlock.gif "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Truncate_Log_File]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[Truncate_Log_File]";
InBlock.gif
InBlock.gif DataTable dt = GetSysFile();
InBlock.gifforeach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(dr["物理名"].ToString().ToLower().LastIndexOf(".ldf")!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif LogFile = dr["逻辑名"].ToString().Trim();
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifstring strCrtProc = String.Format(
InBlock.gif "CREATE PROCEDURE [Truncate_Log_File] AS\n"
InBlock.gif + "SET NOCOUNT ON\n"
InBlock.gif + "DECLARE @LogicalFileName sysname,@MaxMinutes INT,@NewSize INT\n"
InBlock.gif + "SELECT @LogicalFileName ='{0}',@MaxMinutes = 10,@NewSize = {1}\n"
InBlock.gif + "DECLARE @OriginalSize int\n"
InBlock.gif + "SELECT @OriginalSize = size FROM sysfiles WHERE name = @LogicalFileName\n"
InBlock.gif + "DECLARE @Counter INT,@StartTime DATETIME,@TruncLog VARCHAR(255)\n"
InBlock.gif + "WHILE @OriginalSize*8/1024>@Newsize\n"
InBlock.gif + "BEGIN\n"
InBlock.gif + "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[DummyTrans]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)\n"
InBlock.gif + "drop table [dbo].[DummyTrans]\n"
InBlock.gif + "CREATE TABLE DummyTrans\n"
InBlock.gif + "(DummyColumn char (8000) not null)\n"
InBlock.gif + "SELECT @StartTime = GETDATE(),@TruncLog = 'BACKUP LOG ['+ db_name() + '] WITH TRUNCATE_ONLY'\n"
InBlock.gif + "DBCC SHRINKFILE (@LogicalFileName, @NewSize)\n"
InBlock.gif + "EXEC (@TruncLog)\n"
InBlock.gif + "WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE())\n"
InBlock.gif + "AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)\n"
InBlock.gif + "AND (@OriginalSize * 8 /1024) > @NewSize\n"
InBlock.gif + "BEGIN\n"
InBlock.gif + "SELECT @Counter = 0\n"
InBlock.gif + "WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 5000))\n"
InBlock.gif + "BEGIN\n"
InBlock.gif + "INSERT DummyTrans valueS ('Fill Log')\n"
InBlock.gif + "DELETE DummyTrans\n"
InBlock.gif + "SELECT @Counter = @Counter + 1\n"
InBlock.gif + "END\n"
InBlock.gif + "EXEC (@TruncLog)\n"
InBlock.gif + "END\n"
InBlock.gif + "SELECT @OriginalSize=size FROM sysfiles WHERE name = @LogicalFileName\n"
InBlock.gif + "DROP TABLE DummyTrans\n"
InBlock.gif + "END\n",LogFile,NewLogFileSize);
InBlock.gif
InBlock.gifstring strExecProc = "exec Truncate_Log_File";
InBlock.gif
InBlock.gifif(LogFile.Length>0&&this.ExecuteSQL(strDelProc))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(this.ExecuteSQL(strCrtProc))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.ExecuteSQL(strExecProc);
ExpandedSubBlockEnd.gif }

InBlock.gifelse
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gifelse
InBlock.gifreturn false;
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedSubBlockEnd.gif#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif属性#region 属性
InBlock.gif
InBlock.gifpublic bool Active
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.active;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifif(value)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.Connect();
ExpandedSubBlockEnd.gif }

InBlock.gifelse
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.Close();
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string SqlConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn
InBlock.gif "Database=" + this.sqlDefaultDatabase +
InBlock.gif ";Server=" + this.sqlServerName +
InBlock.gif ";User ID=" + this.sqlUserId +
InBlock.gif ";Password=" + this.sqlUserPassword + ";";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string SqlServerName
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.sqlServerName;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.sqlServerName = value;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string SqlUserId
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.sqlUserId;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.sqlUserId = value;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string SqlUserPassword
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.sqlUserPassword;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.sqlUserPassword = value;
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublic string SqlDefaultDatabase
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifget
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn this.sqlDefaultDatabase;
ExpandedSubBlockEnd.gif }

InBlock.gifset
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifthis.sqlDefaultDatabase = value.Replace("'","''");
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif#endregion

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

windows Forms 版本(源码)
asp.net 版本(源码)



本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/05/20/159553.html,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
3天前
|
SQL 存储 关系型数据库
数据库开发之图形化工具以及表操作的详细解析
数据库开发之图形化工具以及表操作的详细解析
19 0
|
28天前
|
SQL 关系型数据库 MySQL
MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复
对于MySQL数据库,可以使用二进制日志(binary log)进行时间点恢复。二进制日志是MySQL中记录所有数据库更改操作的日志文件。要进行时间点恢复,您需要执行以下步骤: 1. 确保MySQL配置文件中启用了二进制日志功能。在配置文件(通常是my.cnf或my.ini)中找到以下行,并确保没有被注释掉: Copy code log_bin = /path/to/binary/log/file 2. 在需要进行恢复的时间点之前创建一个数据库备份。这将作为恢复的基准。 3. 找到您要恢复到的时间点的二进制日志文件和位置。可以通过执行以下命令来查看当前的二进制日志文件和位
|
1月前
|
数据库
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
20 0
|
2月前
|
开发框架 Oracle 关系型数据库
ASP.NET实验室LIS系统源码 Oracle数据库
LIS是HIS的一个组成部分,通过与HIS的无缝连接可以共享HIS中的信息资源,使检验科能与门诊部、住院部、财务科和临床科室等全院各部门之间协同工作。 
37 4
|
2月前
|
运维 安全 关系型数据库
参加数据库管理工具DAS训练营,赢取国潮保温杯和阿里云定制双肩包!
本训练营带您简单了解数据库自治与云安全服务,数据库自治服务提供云上RDS、PolarDB、NoSQL、ADB等数据库7*24小时异常检测、SQL自优化、安全合规审计、弹性伸缩、数据自治、锁分析等亮点功能。一站式自动化、数字化DAS集成平台,助力您畅享DBA运维智能化。
|
1月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
86 1
|
7天前
|
JavaScript Java 测试技术
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
基于Java的公司员工工作日志办公系统的设计与实现(源码+lw+部署文档+讲解等)
31 3
|
7天前
工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断2
工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
15 0
|
8天前
|
机器学习/深度学习 前端开发 数据挖掘
R语言计量经济学:工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
R语言计量经济学:工具变量法(两阶段最小二乘法2SLS)线性模型分析人均食品消费时间序列数据和回归诊断
38 0
|
1月前
|
Oracle 关系型数据库 MySQL
一款好用的数据库表结构文档生成工具
一款好用的数据库表结构文档生成工具

热门文章

最新文章