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.

253 lines
9.4 KiB

using GummingCommon;
using GummingControl;
using GummingEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GummingBusiness
{
public class {ClassName}DA
{
public static int ReadTotalCount(PagerEntity se)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT count(*) FROM {TableName} WHERE IsActive>0 " + se.Conditions;
SQLiteDataReader sr = cmd.ExecuteReader();
sr.Read();
int count = sr.GetInt32(0);
sr.Close();
conn.Close();
return count;
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
return 0;
}
public static {ClassName}Entity Load(string recId)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
DataTable dt = SQLiteHelper.ExecuteDataTable(conn, "SELECT * FROM {TableName} WHERE RecId ='" + recId + "' limit 1");
List<{ClassName}Entity> rows = SQLiteHelper.DataTableToIList<{ClassName}Entity>(dt) as List<{ClassName}Entity>;
conn.Close();
if (rows.Count > 0)
{
return rows[0];
}
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
return null;
}
public static RecordEntity Load(PagerEntity se)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
int count = ReadTotalCount(se);
//se.OrderBy, se.SortOrder, se.PageIndex, se.Rows
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
DataTable dt = SQLiteHelper.ExecuteDataTable(conn, "SELECT * FROM {TableName} WHERE IsActive>0 " + se.Conditions + " order by ModifyTime desc limit " +
Convert.ToString(se.Rows) + " offset " + Convert.ToString((se.PageIndex - 1) * se.Rows));
List<{ClassName}Entity> rows = SQLiteHelper.DataTableToIList<{ClassName}Entity>(dt) as List<{ClassName}Entity>;
conn.Close();
RecordEntity result = new RecordEntity();
result.total = (int)Math.Ceiling(count * 1.0 / se.Rows);
result.page = se.PageIndex;
result.records = count;
result.rows = rows;
return result;
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
return null;
}
public static void Save({ClassName}Entity segment)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
using (SQLiteTransaction tran = conn.BeginTransaction())
{
Save(conn, segment);
tran.Commit();
}
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
}
public static void Save(SQLiteConnection conn, {ClassName}Entity segment)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO {TableName} ({InsertAllFields}) values({All@Fields})";
cmd.Parameters.AddWithValue("@RecId", Guid.NewGuid().ToString());
//{AddWithParameters}
cmd.Parameters.AddWithValue("@OrderBy", segment.OrderBy);
cmd.Parameters.AddWithValue("@CreateBy", Global.CurrentUserCode);
cmd.Parameters.AddWithValue("@CreateTime", DateTime.Now);
cmd.Parameters.AddWithValue("@ModifyBy", Global.CurrentUserCode);
cmd.Parameters.AddWithValue("@ModifyTime", DateTime.Now);
cmd.Parameters.AddWithValue("@IsActive", 1);
cmd.ExecuteNonQuery();
}
public static void Update({ClassName}Entity segment)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
using (SQLiteTransaction tran = conn.BeginTransaction())
{
Update(conn, segment);
tran.Commit();
}
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
}
public static void Update(SQLiteConnection conn, {ClassName}Entity segment)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "UPDATE {TableName} SET {Update__SqlParametersNames}, OrderBy = @OrderBy, ModifyBy = @ModifyBy, ModifyTime = @ModifyTime, IsActive = @IsActive WHERE RecId = @RecId";
cmd.Parameters.AddWithValue("@RecId", segment.RecId);
//{AddWithParameters}
cmd.Parameters.AddWithValue("@OrderBy", segment.OrderBy);
cmd.Parameters.AddWithValue("@ModifyBy", Global.CurrentUserCode);
cmd.Parameters.AddWithValue("@ModifyTime", DateTime.Now);
cmd.Parameters.AddWithValue("@IsActive", 1);
cmd.ExecuteNonQuery();
}
public static void Delete(string recid)
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
Delete(conn, recid);
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
}
public static void Delete(SQLiteConnection conn, string recid)
{
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "delete from {TableName} where recid ='" + recid + "'";
cmd.ExecuteNonQuery();
}
public static void Clear()
{
string dbPath = FilesDirectory.DatabasePath;
if (!File.Exists(dbPath))
{
throw new Exception("database missing!");
}
try
{
SQLiteConnection conn = SQLiteHelper.CreateConnection();
if (conn.State != System.Data.ConnectionState.Open)
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "delete from {TableName}";
cmd.ExecuteNonQuery();
conn.Close();
}
}
catch (Exception ex)
{
throw new Exception("打开数据库:" + dbPath + "的连接失败:" + ex.Message);
}
}
}
}