public static string GetKeyId() { IntPtr key = IntPtr.Zero; SqliteCoreDll.GetAuthorizeId(ref key); return(key.ToAnsiString().Substring(0, 32)); }
/// <summary> /// 获取表定义。 /// 获取表列集合。 /// 获取列数据类型集合。 /// </summary> private static void GetTableDefin(IntPtr dbBase, string tableName, out IList <string> allColumnNames, out IList <string> allColumnTypes) { //获取表定义(包含列名和列类型) IntPtr columnNames = IntPtr.Zero; IntPtr columnTypes = IntPtr.Zero; int columnCount = 0; int gettabledefineCode = SqliteCoreDll.GetTableDefine(dbBase, tableName, ref columnNames, ref columnTypes, ref columnCount); if (gettabledefineCode != 0) { LogHelper.Error(string.Format("获取表【{0}】定义失败,错误码:{1},可能原因:表名字错误", tableName, gettabledefineCode)); allColumnNames = new List <string>(); allColumnTypes = new List <string>(); return; } allColumnNames = ConvertToArray(columnNames, columnCount); allColumnTypes = ConvertToArray(columnTypes, columnCount); int freeTableDefineCode = SqliteCoreDll.FreeTableDefine(dbBase, ref columnNames, ref columnTypes, ref columnCount); if (freeTableDefineCode != 0) { LogHelper.Warn(string.Format("Sqlite数据库恢复-释放表【{0}】定义失败,错误码:{1}", tableName, freeTableDefineCode)); } }
/// <summary> /// 调用底层方法,获取所有用户信息 /// </summary> /// <param name="sourceDb">源数据库路径</param> /// <param name="charatorPath">特征库文件路径</param> /// <returns></returns> public static List <string> ButtomGetAllTables(string sourceDb, string charatorPath) { var tableNames = new List <string>(); IntPtr dbBase = IntPtr.Zero; var stackMsgBuilder = new StringBuilder(); bool isSuccess = true; IntPtr tableArr = IntPtr.Zero; int tableCount = 0; //判断是否初始化,若初始化底层失败,则返回。 if (!InitDb(sourceDb, charatorPath, ref dbBase)) { stackMsgBuilder.AppendLine("SQLite底层DLl初始化失败。可能原因"); stackMsgBuilder.AppendLine("1:程序未使用管理员权限运行。"); stackMsgBuilder.AppendLine("2:底层DLL缺少必要的Key文件。"); isSuccess = false; } if (isSuccess) { SqliteCoreDll.GetAllTableName(dbBase, ref tableArr, ref tableCount); tableNames = ConvertToArray(tableArr, tableCount); } else { LogHelper.Error(stackMsgBuilder.ToSafeString()); } return(tableNames); }
/// <summary> /// 清理资源,释放数据库句柄。 /// 把所有数据清空。 /// </summary> /// <param name="dbBase"></param> private static void DisposeSource(IntPtr dbBase) { if (dbBase != IntPtr.Zero) { int freeDataBase = SqliteCoreDll.CloseSqliteHandle(dbBase); if (freeDataBase != 0) { LogHelper.Error(string.Format("Sqlite数据库恢复-释放数据库句柄失败,错误码:{0}", freeDataBase)); } } _AllNewRowData = null; }
/// <summary> /// 获取所有从Dll底层返回的数据。 /// </summary> /// <param name="dbBase"></param> /// <param name="tableName"></param> private static void GetTableAllData(IntPtr dbBase, string tableName) { //获取表的正常和删除数据。 _AllNewRowData = new List <List <SqliteColumnObject> >(); //int getCotentCode = SqliteCoreDll.getTableContentGenearal(dbBase, _CallBack, tableName, _DataMode); // 这里可以直接调用 SqliteCallBack 为什么还要定义一个变量 _CallBack 呢?而且 _CallBack 也就这个地方使用; int getCotentCode = SqliteCoreDll.getTableContentGenearal(dbBase, SqliteCallBack, tableName, _DataMode); if (getCotentCode != 0) { LogHelper.Error(string.Format("Sqlite数据库恢复-Sqlite底层读取表[{0}]记录发生错误,错误码:{1}", tableName, getCotentCode)); } }
/// <summary> /// 初始化Dll底层 /// 类似与Mount。 /// </summary> /// <param name="sourceDb">源数据库路径。</param> /// <param name="charatorPath">特征库路径。</param> /// <param name="dbBase">数据库句柄。</param> /// <returns>返回是否初始化成功。</returns> private static bool InitDb(string sourceDb, string charatorPath, ref IntPtr dbBase) { try { int initCode = SqliteCoreDll.Init(licenseFile); if (initCode != 0) { LogHelper.Error(string.Format("Sqlite数据库恢复-Sqlite底层初始化错误,错误码:{0}", initCode)); return(false); } int openCode = SqliteCoreDll.OpenSqliteData(ref dbBase, sourceDb, charatorPath); if (openCode != 0) { LogHelper.Error(openCode == 9999 ? string.Format("Sqlite数据库恢复-Sqlite 打开数据库失败,错误码:{0}.原因可能是没有注册或者没有管理员方式运行", openCode) : string.Format("Sqlite数据库恢复-Sqlite 打开数据库失败,错误码:{0}", openCode)); return(false); } int formatCode = 0; int getCode = SqliteCoreDll.GetCodeFomart(dbBase, ref formatCode); if (getCode != 0) { LogHelper.Error(string.Format("-Sqlite数据库恢复Sqlite获取数据库【{0}】编码失败,错误码:{1}", sourceDb, getCode)); } _CurrentEncoding = GetFormatString(formatCode); //_CallBack = SqliteCallBack; return(true); } catch (Exception ex) { LogHelper.Error("调用底层SQLite-dll发生异常", ex); return(false); } }