捕获数据库操作的所有异常

croire 3 years ago
parent 013c6f2fe7
commit 4a3f0d43eb

@ -103,21 +103,30 @@ namespace DownKyi.Core.Storage.Database
{
List<Cover> covers = new List<Cover>();
dbHelper.ExecuteQuery(sql, reader =>
try
{
while (reader.Read())
dbHelper.ExecuteQuery(sql, reader =>
{
Cover cover = new Cover
while (reader.Read())
{
Avid = (long)reader["avid"],
Bvid = (string)reader["bvid"],
Cid = (long)reader["cid"],
Url = (string)reader["url"],
Md5 = (string)reader["md5"]
};
covers.Add(cover);
}
});
Cover cover = new Cover
{
Avid = (long)reader["avid"],
Bvid = (string)reader["bvid"],
Cid = (long)reader["cid"],
Url = (string)reader["url"],
Md5 = (string)reader["md5"]
};
covers.Add(cover);
}
});
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e);
LogManager.Error($"{tableName}", e);
}
return covers;
}

@ -14,7 +14,7 @@ namespace DownKyi.Core.Storage.Database.Download
protected string tableName = "download";
#if DEBUG
private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload().Replace(".db","_debug.db"));
private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload().Replace(".db", "_debug.db"));
#else
private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key);
#endif
@ -33,25 +33,25 @@ namespace DownKyi.Core.Storage.Database.Download
/// <param name="obj"></param>
public void Insert(string uuid, object obj)
{
// 定义一个流
Stream stream = new MemoryStream();
// 定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 序列化
formatter.Serialize(stream, obj);
try
{
// 定义一个流
Stream stream = new MemoryStream();
// 定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 序列化
formatter.Serialize(stream, obj);
byte[] array = null;
array = new byte[stream.Length];
byte[] array = null;
array = new byte[stream.Length];
//将二进制流写入数组
stream.Position = 0;
stream.Read(array, 0, (int)stream.Length);
//将二进制流写入数组
stream.Position = 0;
stream.Read(array, 0, (int)stream.Length);
//关闭流
stream.Close();
//关闭流
stream.Close();
try
{
string sql = $"insert into {tableName}(id, data) values (@id, @data)";
dbHelper.ExecuteNonQuery(sql, new Action<SQLiteParameterCollection>((para) =>
{
@ -86,25 +86,25 @@ namespace DownKyi.Core.Storage.Database.Download
public void Update(string uuid, object obj)
{
// 定义一个流
Stream stream = new MemoryStream();
// 定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 序列化
formatter.Serialize(stream, obj);
try
{
// 定义一个流
Stream stream = new MemoryStream();
// 定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 序列化
formatter.Serialize(stream, obj);
byte[] array = null;
array = new byte[stream.Length];
byte[] array = null;
array = new byte[stream.Length];
//将二进制流写入数组
stream.Position = 0;
stream.Read(array, 0, (int)stream.Length);
//将二进制流写入数组
stream.Position = 0;
stream.Read(array, 0, (int)stream.Length);
//关闭流
stream.Close();
//关闭流
stream.Close();
try
{
string sql = $"update {tableName} set data=@data where id glob @id";
dbHelper.ExecuteNonQuery(sql, new Action<SQLiteParameterCollection>((para) =>
{
@ -160,22 +160,31 @@ namespace DownKyi.Core.Storage.Database.Download
{
Dictionary<string, object> objects = new Dictionary<string, object>();
dbHelper.ExecuteQuery(sql, reader =>
try
{
while (reader.Read())
dbHelper.ExecuteQuery(sql, reader =>
{
// 读取字节数组
byte[] array = (byte[])reader["data"];
// 定义一个流
MemoryStream stream = new MemoryStream(array);
//定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 反序列化
object obj = formatter.Deserialize(stream);
objects.Add((string)reader["id"], obj);
}
});
while (reader.Read())
{
// 读取字节数组
byte[] array = (byte[])reader["data"];
// 定义一个流
MemoryStream stream = new MemoryStream(array);
//定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 反序列化
object obj = formatter.Deserialize(stream);
objects.Add((string)reader["id"], obj);
}
});
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e);
LogManager.Error($"{tableName}", e);
}
return objects;
}

@ -90,20 +90,29 @@ namespace DownKyi.Core.Storage.Database
{
List<Header> headers = new List<Header>();
dbHelper.ExecuteQuery(sql, reader =>
try
{
while (reader.Read())
dbHelper.ExecuteQuery(sql, reader =>
{
Header header = new Header
while (reader.Read())
{
Mid = (long)reader["mid"],
Name = (string)reader["name"],
Url = (string)reader["url"],
Md5 = (string)reader["md5"]
};
headers.Add(header);
}
});
Header header = new Header
{
Mid = (long)reader["mid"],
Name = (string)reader["name"],
Url = (string)reader["url"],
Md5 = (string)reader["md5"]
};
headers.Add(header);
}
});
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e);
LogManager.Error($"{tableName}", e);
}
return headers;
}

Loading…
Cancel
Save