更新一些数据库写法
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Data;
|
||||
using System.Data.SQLite;
|
||||
using static NPOI.HSSF.Util.HSSFColor;
|
||||
|
||||
namespace Ramitta
|
||||
{
|
||||
@@ -9,14 +10,14 @@ namespace Ramitta
|
||||
private bool disposed = false;
|
||||
|
||||
// 构造函数,初始化数据库连接
|
||||
// 参数: connectionString - 数据库连接字符串
|
||||
// 例如: "Data Source=mydatabase.db;Version=3;"
|
||||
public SQLite(string connectionString)
|
||||
public SQLite(string file,bool ReadOnly=false)
|
||||
{
|
||||
db = new SQLiteConnection(connectionString);
|
||||
var sql = $"Data Source={file};Version=3;{(ReadOnly ? "Read Only=True;" : "")}";
|
||||
db = new SQLiteConnection(sql);
|
||||
db.Open();
|
||||
}
|
||||
|
||||
|
||||
// 创建表:根据表名和字段定义创建表
|
||||
// 参数: tableName - 表名
|
||||
// 参数: columns - 字段定义字典,键为字段名,值为字段类型
|
||||
@@ -32,7 +33,26 @@ namespace Ramitta
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
// 获取数据库中所有表名
|
||||
// 返回: 包含所有表名的字符串列表
|
||||
public List<string> GetAllTableNames()
|
||||
{
|
||||
List<string> tableNames = new List<string>();
|
||||
|
||||
string query = "SELECT name FROM sqlite_master WHERE type='table';";
|
||||
|
||||
using (var cmd = new SQLiteCommand(query, db))
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
string tableName = reader.GetString(0);
|
||||
tableNames.Add(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
return tableNames;
|
||||
}
|
||||
// 向已存在的表中添加新列
|
||||
// 参数: tableName - 表名
|
||||
// 参数: columnName - 要添加的列名
|
||||
|
||||
@@ -381,5 +381,14 @@ namespace Ramitta.lib
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成一个新的UUID/GUID
|
||||
/// </summary>
|
||||
/// <returns>格式为: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</returns>
|
||||
public static string GenerateUUID()
|
||||
{
|
||||
return Guid.NewGuid().ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user