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) { }
}
}
}