一点补充

croire 3 years ago
parent fb0fcf1e7e
commit 53be15c7ab

@ -2,6 +2,8 @@
using DownKyi.Core.Storage.Database.Download; using DownKyi.Core.Storage.Database.Download;
using DownKyi.Models; using DownKyi.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.Test.Database namespace DownKyi.Core.Test.Database
{ {
@ -26,5 +28,23 @@ namespace DownKyi.Core.Test.Database
DownloadingDb db = new DownloadingDb(); DownloadingDb db = new DownloadingDb();
db.Insert("testId", downloading); db.Insert("testId", downloading);
} }
[TestMethod]
public void TestMethod2()
{
DownloadingDb db = new DownloadingDb();
Dictionary<string, object> query = db.QueryAll();
Downloading downloading = (Downloading)query["testId"];
Console.WriteLine(downloading.Gid);
Console.WriteLine(downloading.PlayStreamType);
Console.WriteLine(downloading.DownloadContent);
Console.WriteLine(downloading.DownloadStatusTitle);
Console.WriteLine(downloading.Progress);
Console.WriteLine(downloading.DownloadingFileSize);
Console.WriteLine(downloading.MaxSpeed);
Console.WriteLine(downloading.SpeedDisplay);
}
} }
} }

@ -11,15 +11,9 @@ namespace DownKyi.Core.Storage.Database.Download
public class DownloadDb public class DownloadDb
{ {
private const string key = "bdb8eb69-3698-4af9-b722-9312d0fba623"; private const string key = "bdb8eb69-3698-4af9-b722-9312d0fba623";
private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload()); private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key);
//private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key);
protected string tableName = "download"; protected string tableName = "download";
//public DownloadDb()
//{
// CreateTable();
//}
/// <summary> /// <summary>
/// 关闭数据库连接 /// 关闭数据库连接
/// </summary> /// </summary>
@ -72,7 +66,18 @@ namespace DownKyi.Core.Storage.Database.Download
/// </summary> /// </summary>
/// <param name="sql"></param> /// <param name="sql"></param>
/// <returns></returns> /// <returns></returns>
public Dictionary<string, object> QueryAll(string sql) public Dictionary<string, object> QueryAll()
{
string sql = $"select * from {tableName}";
return Query(sql);
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private Dictionary<string, object> Query(string sql)
{ {
Dictionary<string, object> objects = new Dictionary<string, object>(); Dictionary<string, object> objects = new Dictionary<string, object>();
@ -80,7 +85,16 @@ namespace DownKyi.Core.Storage.Database.Download
{ {
while (reader.Read()) while (reader.Read())
{ {
objects.Add((string)reader["id"], reader["data"]); // 读取字节数组
byte[] array = (byte[])reader["data"];
// 定义一个流
MemoryStream stream = new MemoryStream(null);
//定义一个格式化器
BinaryFormatter formatter = new BinaryFormatter();
// 反序列化
object obj = formatter.Deserialize(stream);
objects.Add((string)reader["id"], obj);
} }
}); });
return objects; return objects;
@ -92,7 +106,7 @@ namespace DownKyi.Core.Storage.Database.Download
/// </summary> /// </summary>
protected void CreateTable() protected void CreateTable()
{ {
string sql = $"create table if not exists {tableName} (id varchar(255), data blob)"; string sql = $"create table if not exists {tableName} (id varchar(255) unique, data blob)";
dbHelper.ExecuteNonQuery(sql); dbHelper.ExecuteNonQuery(sql);
} }
} }

@ -1,5 +1,4 @@
using DownKyi.Core.Settings; using DownKyi.Core.Settings;
using DownKyi.Models;
using DownKyi.Services.Download; using DownKyi.Services.Download;
using DownKyi.Utils; using DownKyi.Utils;
using DownKyi.ViewModels; using DownKyi.ViewModels;

Loading…
Cancel
Save