using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using Znyc.Recruitment.Admin.Commons.IDbContext; namespace Znyc.Recruitment.Admin.Commons.DbContextCore { /// /// SQLite数据库操作 /// public class SQLiteDbContext : BaseDbContext, ISQLiteDbContext { /// /// /// /// /// /// public override DataTable GetDataTable(string sql, int cmdTimeout = 30, params DbParameter[] parameters) { return GetDataTables(sql, cmdTimeout, parameters).FirstOrDefault(); } /// /// /// /// /// /// public override List GetDataTables(string sql, int cmdTimeout = 30, params DbParameter[] parameters) { List dts = new List(); //TODO: connection 不能dispose 或者 用using,否则下次获取connection会报错提示“the connectionstring property has not been initialized。” DbConnection connection = Database.GetDbConnection(); if (connection.State != ConnectionState.Open) { connection.Open(); } using (SqliteCommand cmd = new SqliteCommand(sql, (SqliteConnection)connection)) { cmd.CommandTimeout = cmdTimeout; if (parameters != null && parameters.Length > 0) { cmd.Parameters.AddRange(parameters); } using (SqliteDataReader reader = cmd.ExecuteReader()) { dts.Add(reader.GetSchemaTable()); } } connection.Close(); return dts; } } }