using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Stone.Common; using System.Windows.Forms; using Stone.Entity; using Gm_WMS.DataAccess.DataService; namespace Stone.WinBiz.BasicData { public class F_Base { /// /// 基础资料类型 /// public string type = ""; /// /// 基础资料的名称 /// public string name = string.Empty; /// /// 基础资料储存数据的DataSet /// public DataSet dsMain = null; public EntityBase entity = null; public EntityBase entityView = null; public string strOrder = "[ID] asc"; public F_Base() { } /// /// 绑定数据到DataSet中去(分页) /// /// //public virtual void BindPageData(string strWhere, int rows1, int rows2) //{ // string strOrder = "id asc"; // if (entityView == null) entityView = entity; // dsMain = entityView.GetDataLimit(rows1, rows2, "", strWhere, strOrder); // dsMain.Tables[0].TableName = "Data"; // dsMain.Tables.Add(entityView.GetData("count(*) as AllCount", strWhere, "AllCount asc").Tables[0].Copy()); // dsMain.Tables[1].TableName = "Page"; // dsMain.Tables.Add(entity.Table.Copy()); // dsMain.Tables[2].TableName = "Table"; //} public virtual void BindPageData(string strWhere) { //string strOrder = "id asc"; if (entityView == null) entityView = entity; dsMain = entityView.GetData("", strWhere, strOrder); dsMain.Tables[0].TableName = "Data"; dsMain.Tables.Add(entity.Table.Copy()); dsMain.Tables[1].TableName = "Table"; } /// /// 获取DataGridView的显示结果 /// /// public virtual void GetView(DataGridView dgv) { dgv.DataSource = dsMain.Tables["Data"]; dgv.Columns["ID"].Visible = false; //隐藏ID列 MyGridViewStyle.GetGridViewState(dgv, "基础资料" + type); MyGridViewStyle.SetDataGridMenuCommon(dgv); MyGridViewStyle.SetDataGridRowNumber(dgv); } /// /// 最得指定ID的记录 /// /// /// public virtual DataRow GetData(int ID) { DataSet dsData = entity.GetData("", ID); if (dsData.Tables[0].Rows.Count > 0) { return dsData.Tables[0].Rows[0]; } else { return null; } } /// /// 查询指定条件的记录 /// /// /// public virtual DataRow GetData(string strWhere) { DataSet dsData = entity.GetData(strWhere); if (dsData.Tables[0].Rows.Count > 0) { return dsData.Tables[0].Rows[0]; } else { return null; } } /// /// 验证数据是否正确 /// /// public virtual void Checking(DataRow drData, bool isNew) { if (drData["Code"].ToString().Trim() == "") throw new Exception("代码不能为空!"); if (isNew) { if (entity.GetData("", "Code='" + drData["Code"].ToString() + "'", "id asc").Tables[0].Rows.Count > 0) throw new Exception("代码 " + drData["Code"].ToString() + " 已经存在!"); } } /// /// 添加一行数据 /// /// public virtual void Add(DataRow drData) { Checking(drData, true); entity.Add(drData); Stone.WinBiz.BasicData.F_Log.WriteLog("新增数据[" + this.name + "]"); } /// /// 修改一行记录 /// /// public virtual void Edit(DataRow drData) { Checking(drData, false); entity.Edit(drData); Stone.WinBiz.BasicData.F_Log.WriteLog("修改数据[" + this.name + "]"); } /// /// 删除指定ID的记录 /// /// public virtual void Delete(int ID) { entity.Del(ID); //删除DataSet中的记录 DataRow[] drDatas = dsMain.Tables[0].Select("ID=" + ID); if (drDatas.Length > 0) { drDatas[0].Delete(); } Stone.WinBiz.BasicData.F_Log.WriteLog("删除数据[" + this.name + "]"); } /// /// 根据Code获取Name /// /// /// public virtual string GetNameByCode(string Code) { DataSet dsData = entity.GetData("Name", "Code='" + Code + "'", "id asc"); string result = ""; if (dsData.Tables[0].Rows.Count > 0) { result = dsData.Tables[0].Rows[0]["Name"].ToString(); } return result; } /// /// 导入数据 /// /// public virtual void Input(string filename) { DataSet dsInput = null; Stone.Common.MyExcelDatabase.OpenDatabase(filename, true); dsInput = Stone.Common.MyExcelDatabase.ExecuteDataSet("select * from [Sheet1$]"); Stone.Common.MyExcelDatabase.CloseDatabase(); LocalDBService db = null; try { db = new LocalDBService(); db.BeginTrans(); InputData(dsInput, db); db.Commit(); } catch (Exception ex) { if (db != null) db.Rollback(); throw new Exception("导入数据失败!\r\n" + "原因为:\r\n" + ex.Message); } finally { if (db != null) db.EndTrans(); } Stone.WinBiz.BasicData.F_Log.WriteLog("导出Excel[" + this.name + "]"); } public virtual void InputData(DataSet dsData, LocalDBService db) { } } }