sqlite数据库损坏的时候修复


pubic bool Test()

{

   int doFlag = 0;    //检查并且修复的次数
         doSqliteDb:

      try

     {

                //执行SQL数据处理

                ....................................................................

      }

         catch(SQLiteException ex)

        {

              if (ex.Message.Contains("File opened that is not a database")

             {

                   try{

                        CheckAndDoSqliteDb();

                        } catch  catch(SQLiteException ex)

                   //检查并且修复数据库

                   string sqliteDbFile=" 本地sqlite数据库文件地址";

                   CheckAndDoSqliteDb(sqliteDbFile);

                    doSqliteDb++;
                    if (doFlag < 2)
                    {
                        goto doSqliteDb;
                    }

             }

         }

}

 

//数据库检查并且修复

private static object lockFlag = new object();

private static int num = 0;

public  static void CheckAndDoSqliteDb(string path)

     if (string.IsNullOrEmpty(path) || !File.Exists(path)) return;

     lock (lockFlag)

    {

          FileStream fs = null;

          try

         {

                 if (File.GetAttributes(path) != FileAttributes.Normal)            

                {                            

                        File.SetAttributes(path, FileAttributes.Normal);               

                }

                fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, 32, FileOptions.RandomAccess);            

                byte[] buf = new byte[32];              

                fs.Read(buf, 0, buf.Length);

 

                //都是'\0'表示是新建的数据库,头信息里面没有任何数据。这时不执行修复,直接返回。
                if (string.IsNullOrEmpty(Encoding.ASCII.GetString(buf, 0, buf.Length).Replace('\0', ' ').Trim())) {        return;    }

               

                string headStr = Encoding.ASCII.GetString(buf, 0, 16);       

                if (headStr != "SQLite format 3\0")                        

               {

                            StringBuilder msg = new StringBuilder();

                            msg.Append("SQLITE数据库:");

                            msg.Append(path);

                            msg.Append("损坏,已尝试修复。\r\n 损坏时的值:");

                            foreach (byte b in buf)

                            {

                                msg.Append(b.ToString("X"));

                                msg.Append(",");

                            }

                            msg.Remove(msg.Length - 1, 1);

                            msg.Append(";");

                             //记录本地日志

                            byte[] headString = Encoding.ASCII.GetBytes("SQLite format 3\0"); //第1、2个字节的标准值

                            byte[] secondLine = new byte[10] { 0x04, 0x00, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20, 0x00, 0x00 }; //第3、4个字节的标准值                             Array.Copy(headString, buf, headString.Length);

                            Array.Copy(secondLine, 0, buf, 16, secondLine.Length);

                            fs.Seek(0, SeekOrigin.Begin); fs.Write(buf, 0, buf.Length);

                            fs.Flush();

                        }

         }

         catch(Exception ex)

        {

 

                      try {                           

                              if (num < 10)//在运行过程中只记录10次异常                     

                              {                                

                                       num++;                               

                                       StringBuilder errorMsg = new StringBuilder();                    

                                       errorMsg.Append("尝试修复数据库:");                                

                                       errorMsg.Append(path);  msg.Append(",时发生错误。\r\n异常类型:");                            

                                       errorMsg.Append(ex.GetType().Name.ToString());                             

                                       errorMsg.Append("\r\n错误信息:");                                

                                       errorMsg.Append(ex.Message);

                                       //记录本地日志

        }

        finally

       {

         try
                        {
                            if (fs != null)
                            {
                                fs.Close();
                                fs.Dispose();
                            }
                        }  catch (Exception)  {   }

        }

    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM