You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/BaseDataHelper.cs

1067 lines
43 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using ICSharpCode.Core;
using Mesnac.Basic;
using Mesnac.Codd.Session;
using System.Reflection;
namespace Mesnac.Action.ChemicalWeighing
{
/// <summary>
/// 基础数据业务辅助类
/// </summary>
public class BaseDataHelper
{
#region 字段定义
private static Dictionary<string, DataTable> _SysCode = null;
private static Dictionary<string, Dictionary<string, Entity.SysCode>> _dicSysCode = null;
#endregion
#region 码表业务
/// <summary>
/// 全局码表字典
/// </summary>
public static Dictionary<string, DataTable> SysCode
{
get
{
lock (String.Empty)
{
if (_SysCode == null || _SysCode.Count == 0)
{
_SysCode = new Dictionary<string, DataTable>();
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取数据连接失败,请检查数据库连接字符串是否正确!");
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select TypeID from SysCode group by TypeID";
DataTable dtKeys = dbHelper.GetDataTableBySql(sqlstr);
sqlstr = "select * from SysCode order by TypeID, RecordTime";
DataTable dt = dbHelper.GetDataTableBySql(sqlstr);
if (dtKeys != null && dtKeys.Rows.Count > 0)
{
foreach (DataRow row in dtKeys.Rows)
{
string key = Mesnac.Basic.DataProcessor.RowValue(row, "TypeID", String.Empty).Trim();
if (!String.IsNullOrEmpty(key))
{
DataTable table = new DataTable("SysCode_" + key);
foreach (DataColumn col in dt.Columns)
{
table.Columns.Add(col.ColumnName, col.DataType);
}
foreach (DataRow r in dt.Rows)
{
if (Mesnac.Basic.DataProcessor.RowValue(r, "TypeID", String.Empty).Trim() == key)
{
DataRow newRow = table.NewRow();
foreach (DataColumn c in table.Columns)
{
newRow[c.ColumnName] = r[c.ColumnName];
}
table.Rows.Add(newRow);
}
}
_SysCode.Add(key, table);
}
}
}
}
return _SysCode;
}
}
}
/// <summary>
/// 全局码表字典
/// </summary>
public static Dictionary<string, Dictionary<string, Entity.SysCode>> DicSysCode
{
get
{
lock (String.Empty)
{
if (_dicSysCode == null || _dicSysCode.Count == 0)
{
_dicSysCode = new Dictionary<string, Dictionary<string, Entity.SysCode>>();
Entity.SysCode code = null;
foreach (string typeID in SysCode.Keys)
{
_dicSysCode.Add(typeID, new Dictionary<string, Entity.SysCode>());
if (SysCode[typeID] != null && SysCode[typeID].Rows.Count > 0)
{
foreach (DataRow dr in SysCode[typeID].Rows)
{
code = new Entity.SysCode();
code.TypeID = typeID;
code.ItemCode = Mesnac.Basic.DataProcessor.RowValue(dr, "ItemCode", String.Empty);
code.ItemName = Mesnac.Basic.DataProcessor.RowValue(dr, "ItemName", String.Empty);
code.Remark = Mesnac.Basic.DataProcessor.RowValue(dr, "Remark", String.Empty);
_dicSysCode[typeID].Add(code.ItemCode, code);
}
}
}
}
return _dicSysCode;
}
}
}
/// <summary>
/// 根据TypeID更新码表所有备注值
/// </summary>
/// <param name="typeID">类型ID</param>
/// <param name="newRemarkValue">备注新值</param>
public static void UpdateSysCodeRemark(string typeID, string newRemarkValue)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
dbHelper.CommandType = CommandType.Text;
string sqlstr = "update SysCode set Remark = @Remark where TypeID = @TypeID";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@Remark", newRemarkValue);
dbHelper.AddParameter("@TypeID", typeID);
dbHelper.ExecuteNonQuery();
#region 更新缓存
DataTable dt = SysCode[typeID];
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
dr["Remark"] = newRemarkValue;
}
}
Dictionary<string, Entity.SysCode> dic = DicSysCode[typeID];
if (dic != null && dic.Count > 0)
{
foreach (Entity.SysCode sc in dic.Values)
{
sc.Remark = newRemarkValue;
}
}
#endregion
}
/// <summary>
/// 更新码表指定代码的备注值
/// </summary>
/// <param name="typeID">类型ID</param>
/// <param name="itemCode">代码值</param>
/// <param name="newRemarkValue">备注新值</param>
public static void UpdateSysCodeRemark(string typeID, string itemCode, string newRemarkValue)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
dbHelper.CommandType = CommandType.Text;
string sqlstr = "update SysCode set Remark = @Remark where TypeID = @TypeID and ItemCode = @ItemCode";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@Remark", newRemarkValue);
dbHelper.AddParameter("@TypeID", typeID);
dbHelper.AddParameter("@ItemCode", itemCode);
dbHelper.ExecuteNonQuery();
#region 更新缓存
DataTable dt = SysCode[typeID];
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
if (Mesnac.Basic.DataProcessor.RowValue(dr, "ItemCode", String.Empty) == itemCode)
{
dr["Remark"] = newRemarkValue;
break;
}
}
}
Dictionary<string, Entity.SysCode> dic = DicSysCode[typeID];
if (dic != null && dic.Count > 0)
{
foreach (Entity.SysCode sc in dic.Values)
{
if (sc.ItemCode == itemCode)
{
sc.Remark = newRemarkValue;
break;
}
}
}
#endregion
}
#endregion
#region 字典操作
#region 字典更新
/// <summary>
/// 更新本地库SysKeyValue表中对应键的值如果不存在则添加
/// </summary>
/// <param name="key">要更新的键</param>
/// <param name="value">新值</param>
public static void UpdateSysValue(string key, string value)
{
try
{
string strSql = String.Empty;
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("更新SysKeyValue表中对应键的值失败获取本地数据连接失败");
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
if (ExistsKey(key))
{
strSql = "update SysKeyValue set ssValue = '{0}' where ssKey = @ssKey ";
}
else
{
strSql = "insert into SysKeyValue(ssKey,ssValue) values(@ssKey,'{0}')";
}
strSql = String.Format(strSql, value);
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
localHelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("更新SysKeyValue表中对应键的值失败" + ex.Message, ex);
}
}
#endregion
#region 字典获取
#region 获取值
/// <summary>
/// 获取本地库SysKeyValue表中对应键的值
/// </summary>
/// <param name="key">要查找的键</param>
/// <returns>返回对应键的值获取失败返回String.Empty</returns>
public static string GetSysValue(string key)
{
try
{
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的值失败获取本地数据连接失败");
return String.Empty;
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
string strSql = "select top 1 ssValue from SysKeyValue where ssKey = @ssKey";
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
object result = localHelper.ToScalar();
if (result == null || result == System.DBNull.Value)
{
return String.Empty;
}
else
{
return result.ToString();
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的值失败" + ex.Message, ex);
return String.Empty;
}
}
/// <summary>
/// 字典获取string重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetSysValue(string key, string defaultValue)
{
if (ExistsKey(key))
{
string value = GetSysValue(key);
if (String.IsNullOrEmpty(value))
{
return defaultValue;
}
else
{
return value;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取int重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetSysValue(string key, int defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
int intValue = defaultValue;
if (int.TryParse(strValue, out intValue))
{
return intValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取bool重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool GetSysValue(string key, bool defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
bool boolValue = defaultValue;
if (bool.TryParse(strValue, out boolValue))
{
return boolValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取float重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float GetSysValue(string key, float defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
float floatValue = defaultValue;
if (float.TryParse(strValue, out floatValue))
{
return floatValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
/// <summary>
/// 字典获取double重载
/// </summary>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double GetSysValue(string key, double defaultValue)
{
if (ExistsKey(key))
{
string strValue = GetSysValue(key);
double doubleValue = defaultValue;
if (double.TryParse(strValue, out doubleValue))
{
return doubleValue;
}
else
{
return defaultValue;
}
}
else
{
return defaultValue;
}
}
#endregion
#region 获取备注
/// <summary>
/// 获取本地库SysKeyValue表中对应键的备注
/// </summary>
/// <param name="key">要查找的键</param>
/// <returns>返回对应键的备注信息获取失败返回String.Empty</returns>
public static string GetSysRemark(string key)
{
try
{
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的备注失败获取本地数据连接失败");
return String.Empty;
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
string strSql = "select top 1 ssRemark from SysKeyValue where ssKey = @ssKey";
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
object result = localHelper.ToScalar();
if (result == null || result == System.DBNull.Value)
{
return String.Empty;
}
else
{
return result.ToString();
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的备注失败" + ex.Message, ex);
return String.Empty;
}
}
/// <summary>
/// 获取本地库SysKeyValue表中对应键的备注
/// </summary>
/// <param name="key">要查找的键</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应键的备注信息,获取失败返回默认值</returns>
public static string GetSysRemark(string key, string defaultValue)
{
if (ExistsKey(key))
{
string remark = GetSysRemark(key);
if (String.IsNullOrEmpty(remark))
{
return defaultValue;
}
else
{
return remark;
}
}
else
{
return defaultValue;
}
}
#endregion
#endregion
#region 字典判断
/// <summary>
/// 判断SysKeyValue中是否存在对应key的记录
/// </summary>
/// <param name="key">要检索的key</param>
/// <returns>存在返回true否则返回false</returns>
public static bool ExistsKey(string key)
{
try
{
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的值失败获取本地数据连接失败");
return false;
}
localHelper.CommandType = CommandType.Text;
localHelper.ClearParameter();
string strSql = "select count(1) from SysKeyValue where ssKey = @ssKey";
localHelper.CommandText = strSql;
localHelper.AddParameter("@ssKey", key);
object result = localHelper.ToScalar();
if (result == null || result == System.DBNull.Value || Convert.ToInt32(result) <= 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取SysKeyValue表中对应键的值失败" + ex.Message, ex);
return false;
}
}
#endregion
#endregion
#region 操作日志
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="userGUId">用户GUID</param>
/// <param name="userName">用户名</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public static void DBLog(int shiftId, string shiftName, int classId, string className, string userGUId, string userName, string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
try
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取本地数据连接失败...");
return;
}
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
string sqlstr = @"INSERT INTO SysLog(GUID,ShiftID,ShiftName,ClassID,ClassName,UserGUID,UserName,FormGUID,FormText,FunctionGUID,FunctionText,PerformResult,WorkType,OperDest,Remark,RecordTime,IsUpFlag)
VALUES(@GUID,@ShiftID,@ShiftName,@ClassID,@ClassName,@UserGUID,@UserName,@FormGUID,@FormText,@FunctionGUID,@FunctionText,@PerformResult,@WorkType,@OperDest,@Remark,@RecordTime,@IsUpFlag)";
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@GUID", Guid.NewGuid().ToString().ToUpper());
dbHelper.AddParameter("@ShiftName", shiftName);
dbHelper.AddParameter("@ShiftID", shiftId);
dbHelper.AddParameter("@ClassID", classId);
dbHelper.AddParameter("@ClassName", className);
dbHelper.AddParameter("@UserGUID", userGUId);
dbHelper.AddParameter("@UserName", userName);
dbHelper.AddParameter("@FormGUID", formGUID);
dbHelper.AddParameter("@FormText", formText);
dbHelper.AddParameter("@FunctionGUID", functionGUID);
dbHelper.AddParameter("@FunctionText", functionText);
dbHelper.AddParameter("@PerformResult", performResult);
dbHelper.AddParameter("@WorkType", worktype);
dbHelper.AddParameter("@OperDest", operdest);
dbHelper.AddParameter("@Remark", remark);
dbHelper.AddParameter("@RecordTime", DateTime.Now);
dbHelper.AddParameter("@IsUpFlag", 0);
dbHelper.ExecuteNonQuery();
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("数据库日志记录异常:" + ex.Message, ex);
}
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public static void DBLog(int shiftId, string shiftName, int classId, string className, string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, worktype, operdest, remark);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="shiftId">班次Id</param>
/// <param name="shiftName">班次</param>
/// <param name="classId">班组Id</param>
/// <param name="className">班组</param>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
public static void DBLog(int shiftId, string shiftName, int classId, string className, string formGUID, string formText, string functionGUID, string functionText, string performResult)
{
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, formText, functionText, String.Empty);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
/// <param name="operdest">操作描述</param>
/// <param name="remark">备注</param>
public static void DBLog(string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype, string operdest, string remark)
{
int shiftId = GetSysValue("PlanLog.CurrentShiftID", 0); //获取当前班次
string shiftName = GetSysValue("PlanLog.CurrentShiftName", String.Empty); //获取当前班次名称
int classId = Mesnac.Basic.UserInfo.Instance.ClassID; //获取当前班组
string className = GetSysValue("PlanLog.CurrentClassName", String.Empty); //获取当前班组名称
DBLog(shiftId, shiftName, classId, className, Mesnac.Basic.UserInfo.Instance.UserGUID, Mesnac.Basic.UserInfo.Instance.UserName, formGUID, formText, functionGUID, functionText, performResult, worktype, operdest, remark);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
/// <param name="performResult">执行结果</param>
/// <param name="worktype">操作类型</param>
public static void DBLog(string formGUID, string formText, string functionGUID, string functionText, string performResult, string worktype)
{
DBLog(formGUID, formText, functionGUID, functionText, performResult, worktype, formText, functionText);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="formGUID">窗体GUID</param>
/// <param name="formText">窗体文本</param>
/// <param name="functionGUID">功能GUID</param>
/// <param name="functionText">功能文本</param>
public static void DBLog(string formGUID, string formText, string functionGUID, string functionText)
{
DBLog(formGUID, formText, functionGUID, functionText, formText, functionText);
}
/// <summary>
/// 数据库日志记录
/// </summary>
/// <param name="operateType">操作类别</param>
/// <param name="caption">日志标题</param>
/// <param name="content">日志内容</param>
public static void DBLog(string operateType, string caption, string content)
{
DBLog(String.Empty, String.Empty, String.Empty, String.Empty, String.Empty, operateType, caption, content);
}
#endregion
#region 数据业务
/// <summary>
/// 根据GUID获取某个表的数据行
/// </summary>
/// <param name="tableName">表名</param>
/// <param name="guid">GUID</param>
/// <returns>返回对应的数据行</returns>
public static DataRow GetDataRowByTableAndGUID(string tableName, string guid)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取数据连接失败,请检查数据库连接字符串是否正确!");
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"SELECT * FROM {0} WHERE ID = @ID";
sqlstr = String.Format(sqlstr, tableName);
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@ID", guid);
DataTable dt = dbHelper.ToDataTable();
if (dt != null && dt.Rows.Count > 0)
{
return dt.Rows[0];
}
else
{
return null;
}
}
/// <summary>
/// 克隆数据表
/// </summary>
/// <param name="dt">要克隆的数据表</param>
/// <returns>返回克隆好的数据表</returns>
public static DataTable CloneDataTable(DataTable dt)
{
DataTable table = new DataTable();
foreach (DataColumn c in dt.Columns)
{
table.Columns.Add(c.ColumnName, c.DataType);
}
foreach (DataRow r in dt.Rows)
{
DataRow dr = table.NewRow();
foreach (DataColumn c in dt.Columns)
{
dr[c.ColumnName] = r[c.ColumnName];
}
table.Rows.Add(dr);
}
return table;
}
/// <summary>
/// 根据表名获取表结构
/// </summary>
/// <param name="tableName">表名</param>
/// <returns>返回空表</returns>
public static DataTable GetTableSchemaByName(string tableName)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<BaseDataHelper>.Error("获取数据连接失败,请检查数据库连接字符串是否正确!");
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select * from {0} where 1 = 2";
sqlstr = String.Format(sqlstr, tableName);
DataTable dt = dbHelper.GetDataTableBySql(sqlstr);
return dt;
}
#endregion
#region 界面业务
#region 初始化网格控件的上下文菜单
#region 实现部分
/// <summary>
/// 初始化上下文菜单栏
/// </summary>
/// <returns></returns>
public static ContextMenuStrip InitGridViewContextMenu()
{
ContextMenuStrip Result = new ContextMenuStrip();
Result.ResumeLayout(false);
ToolStripMenuItem tsmiInsert = new ToolStripMenuItem();
tsmiInsert.Name = "ToolStripMenuItem1";
tsmiInsert.Size = new System.Drawing.Size(98, 22);
tsmiInsert.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Feeding_Technical_PmtRecipe_InitFormAction_ContextMenu_Insert")); //插入
tsmiInsert.Click += delegate(object sender, EventArgs e)
{
DataGridView grid = ((ContextMenuStrip)(((ToolStripItem)(sender)).Owner)).SourceControl as DataGridView;
if (grid != null)
{
DataTable oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
#region 禁用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged -= Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
InsertGridViewRow(grid);
#region 把操作入Undo栈
DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
Mesnac.Basic.Service.OperationCommand cmd = new Mesnac.Basic.Service.OperationCommand(grid, dtNew, oldValue);
Mesnac.Basic.Service.UndoRedoService.UndoStack.Push(cmd);
#endregion
#region 启用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged += Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
}
};
ToolStripMenuItem tsmiDelete = new ToolStripMenuItem();
tsmiDelete.Name = "ToolStripMenuItem2";
tsmiDelete.Size = new System.Drawing.Size(98, 22);
tsmiDelete.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Feeding_Technical_PmtRecipe_InitFormAction_ContextMenu_Delete")); //删除
tsmiDelete.Click += delegate(object sender, EventArgs e)
{
DataGridView grid = ((ContextMenuStrip)(((ToolStripItem)(sender)).Owner)).SourceControl as DataGridView;
if (grid != null)
{
DataTable oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
#region 禁用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged -= Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
DeleteGridViewRow(grid);
#region 把操作入Undo栈
DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
Mesnac.Basic.Service.OperationCommand cmd = new Mesnac.Basic.Service.OperationCommand(grid, dtNew, oldValue);
Mesnac.Basic.Service.UndoRedoService.UndoStack.Push(cmd);
#endregion
#region 启用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged += Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
}
};
ToolStripMenuItem tsmiMoveUp = new ToolStripMenuItem();
tsmiMoveUp.Name = "ToolStripMenuItem3";
tsmiMoveUp.Size = new System.Drawing.Size(98, 22);
tsmiMoveUp.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Feeding_Technical_PmtRecipe_InitFormAction_ContextMenu_MoveUp")); //上移
tsmiMoveUp.Click += delegate(object sender, EventArgs e)
{
DataGridView grid = ((ContextMenuStrip)(((ToolStripItem)(sender)).Owner)).SourceControl as DataGridView;
if (grid != null)
{
DataTable oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
#region 禁用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged -= Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
MoveUpGridViewRow(grid);
#region 把操作入Undo栈
DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
Mesnac.Basic.Service.OperationCommand cmd = new Mesnac.Basic.Service.OperationCommand(grid, dtNew, oldValue);
Mesnac.Basic.Service.UndoRedoService.UndoStack.Push(cmd);
#endregion
#region 启用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged += Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
}
};
ToolStripMenuItem tsmiMoveDown = new ToolStripMenuItem();
tsmiMoveDown.Name = "ToolStripMenuItem4";
tsmiMoveDown.Size = new System.Drawing.Size(98, 22);
tsmiMoveDown.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Action_Feeding_Technical_PmtRecipe_InitFormAction_ContextMenu_MoveDown")); //下移
tsmiMoveDown.Click += delegate(object sender, EventArgs e)
{
DataGridView grid = ((ContextMenuStrip)(((ToolStripItem)(sender)).Owner)).SourceControl as DataGridView;
if (grid != null)
{
DataTable oldValue = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
#region 禁用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged -= Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
MoveDownGridViewRow(grid);
#region 把操作入Undo栈
DataTable dtNew = Mesnac.Basic.DataProcessor.GetDataTableFromGridView(grid);
Mesnac.Basic.Service.OperationCommand cmd = new Mesnac.Basic.Service.OperationCommand(grid, dtNew, oldValue);
Mesnac.Basic.Service.UndoRedoService.UndoStack.Push(cmd);
#endregion
#region 启用单元格更改事件
if (Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler.ContainsKey(grid))
{
grid.CellValueChanged += Mesnac.Basic.Service.UndoRedoService.DicControlEventHandler[grid].DataGridView_CellValueChanged;
}
#endregion
}
};
Result.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
tsmiInsert,
tsmiDelete,
tsmiMoveUp,
tsmiMoveDown});
Result.Name = "MixingMenu";
return Result;
}
#endregion
#region 辅助部分
/// <summary>
/// 删除网格行数据
/// </summary>
/// <param name="grid"></param>
private static void DeleteGridViewRow(DataGridView grid)
{
DataGridViewRow row = grid.SelectedRows[0];
for (int i = row.Index; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].Value = i + 1;
}
for (int i = 1; i < grid.ColumnCount; i++)
{
grid.Rows[row.Index].Cells[i].Value = DBNull.Value;
}
grid.Rows[row.Index].Cells[0].Value = grid.Rows.Count;
ResetGridViewSort(grid);
}
/// <summary>
/// 插入网格行数据
/// </summary>
/// <param name="grid"></param>
private static void InsertGridViewRow(DataGridView grid)
{
DataGridViewRow row = grid.SelectedRows[0];
#region 取当前行的上一行,目的是在当前行的上一行添加新行
if (row.Index > 0)
{
row = grid.Rows[row.Index - 1];
}
#endregion
for (int i = row.Index + 1; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].Value = i + 2;
}
for (int i = 1; i < grid.ColumnCount; i++)
{
grid.Rows[grid.Rows.Count - 1].Cells[i].Value = DBNull.Value;
}
grid.Rows[grid.Rows.Count - 1].Cells[0].Value = row.Index + 2;
ResetGridViewSort(grid);
}
/// <summary>
/// 下移网格行数据
/// </summary>
/// <param name="grid"></param>
private static void MoveDownGridViewRow(DataGridView grid)
{
DataGridViewRow row = grid.SelectedRows[0];
if (row.Index == grid.Rows.Count - 1)
{
return;
}
grid.Rows[row.Index].Cells[0].Value = row.Index + 2;
grid.Rows[row.Index + 1].Cells[0].Value = row.Index + 1;
ResetGridViewSort(grid);
}
/// <summary>
/// 上移网格行数据
/// </summary>
/// <param name="grid"></param>
private static void MoveUpGridViewRow(DataGridView grid)
{
DataGridViewRow row = grid.SelectedRows[0];
if (row.Index == 0)
{
return;
}
grid.Rows[row.Index].Cells[0].Value = row.Index;
grid.Rows[row.Index - 1].Cells[0].Value = row.Index + 1;
ResetGridViewSort(grid);
}
/// <summary>
/// 重置网格排序
/// </summary>
/// <param name="grid"></param>
private static void ResetGridViewSort(DataGridView grid)
{
grid.Sort(grid.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
#region 重置序号
for (int i = 0; i < grid.Rows.Count; i++)
{
grid.Rows[i].Cells[0].Value = i + 1;
}
grid.AutoScrollOffset = new System.Drawing.Point(0, 0);
#endregion
}
#endregion
#endregion
#endregion
}
}