招聘后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
2.1 KiB

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
{
/// <summary>
/// SQLite数据库操作
/// </summary>
public class SQLiteDbContext : BaseDbContext, ISQLiteDbContext
{
/// <summary>
/// </summary>
/// <param name="sql"></param>
/// <param name="cmdTimeout"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public override DataTable GetDataTable(string sql, int cmdTimeout = 30, params DbParameter[] parameters)
{
return GetDataTables(sql, cmdTimeout, parameters).FirstOrDefault();
}
/// <summary>
/// </summary>
/// <param name="sql"></param>
/// <param name="cmdTimeout"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public override List<DataTable> GetDataTables(string sql, int cmdTimeout = 30, params DbParameter[] parameters)
{
List<DataTable> dts = new List<DataTable>();
//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;
}
}
}