SQL日志清理SQLcc源码公开,免费获取

声明:

鑫源是此软件的开发者,也是堪博创始人,SQL日志清理专家源码,在此文中公开,源码文章末尾免费获取。该软件已获取软件著作权,但今,本人无偿免费公开。帮助更多软件开发者,软件编程初学者学习和工作应用。

正式公布前,提一下,这个UI 是笔记怀念当年金山毒霸V 的 水晶风格界面,借鉴而来。

D1

D2

D3

SQL日志清理的主界面:

3

1

 

一、SQL清理功能的核心源码:

private void DoingSQLcc(string dbname)
{

            DoDB dodb = new DoDB();
            string logdbname = null;

            try
            {
                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "1");
                System.Threading.Thread.Sleep(1000);

                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "2");
                dodb.SQL_logdb_Name(dbname, out logdbname);
                System.Threading.Thread.Sleep(1000);


                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "3");
                dodb.SQL_logdb_clear(dbname, logdbname);
                System.Threading.Thread.Sleep(5000);

                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "4");
                System.Threading.Thread.Sleep(1000);

                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "5");
          
            }
            catch (Exception ex)
            {
                BeginInvoke(new deleg_UIshowAlert(ShowDoStep), "6");
                MessageBox.Show("操作失败。\r\n\r\n"+ex.Message);//BeginInvoke(new deleg_UIshowAlert(ShowDoStep), ex.Message);
            }
            finally
            {

            }
}

 

以上做对最主要的功能是:
第13行:dodb.SQL_logdb_Name() 作用是:定位到清理的数据库
第18行:dodb.SQL_logdb_clear() 作用是:执行清理的TSQL脚本
以下是13行 18行这两个函数详细代码片段
public bool SQL_logdb_Name(string dbname, out string logdbname)
{
            SqlConnection con = null;
            SqlDataAdapter da = null;
            try
            {
                ConnOnce conOne = new ConnOnce();
                conOne.uidStr = Conn.newInstance().uid;
                conOne.pwdStr = Conn.newInstance().pwd;
                conOne.ipStr = Conn.newInstance().sqlIP;
                conOne.portStr = Conn.newInstance().sqlPort;
                conOne.authorizationType = Conn.newInstance().authorizationType;
                con = new SqlConnection(GetConnStr(conOne, Conn.newInstance().authorizationType));// // con = new SqlConnection(GetConnStr(Conn.newInstance()));
                da = new SqlDataAdapter("USE [" + dbname + "]; exec sp_helpfile", con);
                DataTable tbb = new DataTable();
                da.Fill(tbb);
                if (tbb.Select("usage = 'log only'").Length >= 1)
                {
                    logdbname = tbb.Select("usage = 'log only'")[0]["name"].ToString();
                    return true;
                }
                logdbname = null;
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (da != null)
                {
                    da.Dispose();
                    da = null;
                }
                if (con != null)
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();
                    con = null;
                }
            }
}
public bool SQL_logdb_clear(string dbname, string logdbname)
{
            StringBuilder sb = new StringBuilder();
            sb.Append("USE[master]   ");
            //sb.Append("GO;  ");
            sb.Append(string.Format("ALTER DATABASE [{0}] SET RECOVERY SIMPLE WITH NO_WAIT   ", dbname));
            //sb.Append("GO;  ");
            sb.Append(string.Format("ALTER DATABASE [{0}] SET RECOVERY SIMPLE   ", dbname));
            //sb.Append("GO;  ");
            sb.Append(string.Format("USE [{0}]      ", dbname));
            //sb.Append("GO;  ");
            sb.Append(string.Format("DBCC SHRINKFILE (N'{0}' , 1, TRUNCATEONLY)  ", logdbname.Trim()));//SQL2000 名字会带空格的 要去掉trim()
            //sb.Append("GO;  ");
            sb.Append("USE[master]   ");
            //sb.Append("GO;  ");
            sb.Append(string.Format("ALTER DATABASE [{0}] SET RECOVERY FULL  WITH NO_WAIT   ", dbname));
            //sb.Append("GO;  ");
            sb.Append(string.Format("ALTER DATABASE [{0}] SET RECOVERY FULL   ", dbname));
            //sb.Append("GO;  ");
            SqlConnection con = null;
            SqlCommand cmd = null;
            try
            {
                ConnOnce conOne = new ConnOnce();
                conOne.uidStr = Conn.newInstance().uid;
                conOne.pwdStr = Conn.newInstance().pwd;
                conOne.ipStr = Conn.newInstance().sqlIP;
                conOne.portStr = Conn.newInstance().sqlPort;
                conOne.authorizationType = Conn.newInstance().authorizationType;
                con = new SqlConnection(GetConnStr(conOne, Conn.newInstance().authorizationType));// // con = new SqlConnection(GetConnStr(Conn.newInstance()));
                cmd = new SqlCommand(sb.ToString(), con);
                cmd.CommandTimeout = 80000;
                con.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sb = null;
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (con != null)
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();
                    con = null;
                }
            }

}
上述代码中多次调用 BeginInvoke(new deleg_UIshowAlert(ShowDoStep) 
初学者会问,直接调用ShowDoStep就可以,为什么前面要加BeginInvoke,和deleg_UIshowAlert 。
答案是:为了让界面不卡死! 这两个函数嵌套使用,界面上就会有变化,鼠标拖动窗体更不会冻住
private void ShowDoStep(string str)
{
            if (str == "1")
            {
                label1.Text = label1.Text + " " +_dbName;
            }
            else   if (str == "2")
            {
                pic1.Image = Properties.Resources.icon_suc;
                pic2.Image = Properties.Resources.crystalArr;
                label2.Font= label1.Font;
                label1.Font = Control.DefaultFont;
            }
            else if (str == "3")
            {
                pic2.Image = Properties.Resources.icon_suc;
                pic3.Image = Properties.Resources.crystalArr;
                label3.Font = label2.Font;
                label2.Font = Control.DefaultFont;
            }
            else if (str == "4")
            {
                pic3.Image = Properties.Resources.icon_suc;
                pic4.Image = Properties.Resources.crystalArr;
                label4.Font = label3.Font;
                label3.Font = Control.DefaultFont;
            }
            else if (str == "5")
            {
                pic4.Image = Properties.Resources.icon_suc;
                label4.Font = Control.DefaultFont;
                Text = "清理结束";
                pnlHeadFinal.Visible = true;
             
                pnlShowAtOver.Visible = true;
                tmrAnim.Enabled = true;
            }
            else if (str == "6")
            {
                pic2.Image = Properties.Resources.icon_suc;
                label2.Font = Control.DefaultFont;
                pic3.Image = Properties.Resources.exlaimError;
                label3.Font = Control.DefaultFont;
                pic4.Image = Properties.Resources.exlaimError;
                label4.Font = Control.DefaultFont;
                label5.Text = Text = "清理未成功";
                pnlHeadFinal.Visible = true;
               
                btnClose.Visible = true;
            }
}
以上是从最核心的功能代码。
免费获取完整代码: 在 微信公众号(ID: canbloom)输入 SQL日志清理源码,免费获取
4
posted @ 2026-10-03 14:57  鑫_源  阅读(4)  评论(0)    收藏  举报