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

此博客为转载内容,原文链接为https://www.cnblogs.com/Chinasf/archive/2005/05/20/159553.html ,涉及数据库和操作系统领域。
这几天做测试工作,事务很多,日志很大硬盘很快没空间了,把缩小数据库日志的存储过程封装成一个小工具;这个是压缩日志的代码;觉得手工处理日志麻烦的可以考虑用这个工具试试.

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

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

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

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

InBlock.gif        public DataAccess()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.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.gif        public bool Connect(string ConnectionString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sqlConn.ConnectionString = ConnectionString;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlConn.Open();
InBlock.gif                
this.active = true;
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.active = false;
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public bool Connect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sqlConn.ConnectionString = 
this.SqlConnectionString;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlConn.Open();
InBlock.gif                
this.active = true;
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.active = false;
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public bool Close()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(sqlConn.State!=System.Data.ConnectionState.Closed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    sqlConn.Close();
InBlock.gif                    
this.active = !this.active;
InBlock.gif                    
return true;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else 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.gif        public bool ExecuteSQL(string strSQL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.active) return false;
InBlock.gif
InBlock.gif            SqlCommand sqlCmd = 
new SqlCommand(strSQL,sqlConn);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlCmd.ExecuteNonQuery();
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 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.gif        public DataTable ExecuteSQL(string strSQL,string TableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.active) return null;
InBlock.gif
InBlock.gif            System.Data.SqlClient.SqlDataAdapter sqlDa = 
new SqlDataAdapter(strSQL,sqlConn);
InBlock.gif            DataSet ds = 
new DataSet();
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlDa.Fill( ds,TableName );
InBlock.gif                
return ds.Tables[TableName];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public bool UseMaster()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.ExecuteSQL("use master");
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public bool UseDefaultDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.ExecuteSQL("use " + this.sqlDefaultDatabase);
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public DataTable GetDatabase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.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.gif            
return ExecuteSQL(sql,"sysdatabases");
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public DataTable GetSysFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.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.gif            
return ExecuteSQL(sql,"sysfiles");
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        public bool ExecuteTruncateLog(int NewLogFileSize)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string LogFile = "";
InBlock.gif            
string 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.gif            
foreach(DataRow dr in dt.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(dr["物理名"].ToString().ToLower().LastIndexOf(".ldf")!=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    LogFile = dr["逻辑名"].ToString().Trim();
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string 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.gif            
string strExecProc = "exec Truncate_Log_File";
InBlock.gif
InBlock.gif            
if(LogFile.Length>0&&this.ExecuteSQL(strDelProc))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(this.ExecuteSQL(strCrtProc))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return this.ExecuteSQL(strExecProc);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
属性#region 属性
InBlock.gif
InBlock.gif        
public bool Active
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.active;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Connect();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SqlConnectionString 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return
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.gif        
public string SqlServerName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.sqlServerName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.sqlServerName = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SqlUserId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.sqlUserId;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.sqlUserId = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SqlUserPassword
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.sqlUserPassword;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.sqlUserPassword = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SqlDefaultDatabase
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.sqlDefaultDatabase;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.sqlDefaultDatabase = value.Replace("'","''");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

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

转载于:https://www.cnblogs.com/Chinasf/archive/2005/05/20/159553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值