protected void btnAddAsset_Click(object sender, EventArgs e) { if (IsPageValid()) { try { AssetInfo asset = new AssetInfo(); asset.UnitName = ddlUnit.SelectedItem.Text.ToString(); asset.DepartName = ddlDepart.SelectedItem.Text.ToString(); asset.SiteName = ddlSite.SelectedItem.Text.ToString(); asset.AssetName = txtAssetName.Text.ToString(); asset.Amount = txtAmount.Text.ToString(); asset.Price = txtPrice.Text.ToString(); asset.Memo = txtMemo.Text.ToString(); asset.Type = txtType.Text.ToString(); asset.Save(); Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('��ӳɹ���');</script>"); } catch (Exception Ex) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script language='javascript'>alert('���ʧ�ܣ�');</script>"); } } }
public AssetInfo GetAssetInfoById(int AssetId) { AssetInfo assetInfo = null;// try { assetInfo = new AssetInfo(AssetId); } catch (AppException) { return null; } return assetInfo; }
public object SaveAssetInfo(AssetInfo assetInfo) { assetInfo.Save(); return assetInfo . AssetId; }
public void DeleteById(AssetInfo pAssetInfo) { AssetInfo assetInfo = new AssetInfo(pAssetInfo.AssetId); assetInfo.Delete(); }
protected void btnInExecel_Click(object sender, EventArgs e) { if (myFile.PostedFile.FileName != "") { //�ϴ��ļ��ľ���·�� string sFile = myFile.PostedFile.FileName; //��ȡ�ļ�ȫ�� sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1); //��ȡ���� sFile = sFile.Substring(sFile.LastIndexOf(".")); if (sFile.ToLower() != ".xls") { Page.RegisterStartupScript("", "<script>alert('�ļ���ʽ����ȷ')</script>"); } //Ϊ�˷�ֹ�������������Ϊ�ļ���������ʱ������� string datatime = System.DateTime.Now.ToString("yyyMMddHHmmssffff"); //�ϴ����ļ������� sFile = datatime + sFile; //AppDomain.CurrentDomain.BaseDirectory.ToString() ��ȡ����Ŀ�ĸ�Ŀ¼ //sPath ��ȡ�ϴ����·�� string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "ExcelFiles\\" + sFile; //�ϴ��ļ� myFile.PostedFile.SaveAs(sPath); string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(connStr); if (conn.State.ToString() == "Closed") { conn.Open(); } OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", conn); DataSet ds = new DataSet(); oda.Fill(ds); conn.Close(); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { string strAssetName = ds.Tables[0].Rows[i][0].ToString(); string strAmount = ds.Tables[0].Rows[i][1].ToString(); string strUnitName = ds.Tables[0].Rows[i][2].ToString(); string strDepartName = ds.Tables[0].Rows[i][3].ToString(); string strSiteName = ds.Tables[0].Rows[i][4].ToString(); string strPrice = ds.Tables[0].Rows[i][5].ToString(); string strType = ds.Tables[0].Rows[i][6].ToString(); string strMemo = ds.Tables[0].Rows[i][7].ToString(); try { AssetInfo asset = new AssetInfo(); asset.AssetName = strAssetName.ToString(); asset.Amount = strAmount.ToString(); asset.UnitName = strUnitName.ToString(); asset.DepartName = strDepartName.ToString(); asset.SiteName = strSiteName.ToString(); asset.Price = strPrice.ToString(); asset.Type = strType.ToString(); asset.Memo = strMemo.ToString(); asset.Save(); } catch (Exception ex) { Response.Write("����ʧ��!���ڣ�" + ex.Message); } } Response.Write("<SCRIPT>alert('�����ѳɹ����뵽���ݿ⣡');</SCRIPT>"); gvDataBind(); File.Delete(sPath); } else { Response.Write("<SCRIPT>alert('��ѡ��Excel�ļ���');</SCRIPT>"); } }
protected void gvAsset_RowUpdating(object sender, GridViewUpdateEventArgs e) { int Assetid = Convert.ToInt32(gvAsset.DataKeys[e.RowIndex].Value); TextBox txtAssetName = gvAsset.Rows[e.RowIndex].FindControl("txtAssetName") as TextBox; TextBox txtAmount = gvAsset.Rows[e.RowIndex].FindControl("txtAmount") as TextBox; TextBox txtPrice = gvAsset.Rows[e.RowIndex].FindControl("txtPrice") as TextBox; TextBox txtType = gvAsset.Rows[e.RowIndex].FindControl("txtType") as TextBox; TextBox txtMemo = gvAsset.Rows[e.RowIndex].FindControl("txtMemo") as TextBox; DropDownList ddlUnit = gvAsset.Rows[e.RowIndex].FindControl("ddlUnit") as DropDownList; DropDownList ddlDepart = gvAsset.Rows[e.RowIndex].FindControl("ddlDepart") as DropDownList; DropDownList ddlSite = gvAsset.Rows[e.RowIndex].FindControl("ddlSite") as DropDownList; try { AssetInfo asset = new AssetInfo(Assetid); asset.AssetName = txtAssetName.Text.ToString(); asset.Amount = txtAmount.Text.ToString(); asset.Price = txtPrice.Text.ToString(); asset.Type = txtType.Text.ToString(); asset.Memo = txtMemo.Text.ToString(); asset.UnitName = ddlUnit.SelectedItem.Text.ToString(); asset.DepartName = ddlDepart.SelectedItem.Text.ToString(); asset.SiteName = ddlSite.SelectedItem.Text.ToString(); asset.Save(); gvAsset.EditIndex = -1; gvDataBind(); } catch(Exception Ex) { ClientScript.RegisterStartupScript(this.GetType(), "Save", "alert('����ʧ�ܣ�" + Ex.Message + "');", true); } }
//数据持久化 internal static void SaveToDb(AssetInfo pAssetInfo, Asset pAsset,bool pIsNew) { pAsset.AssetId = pAssetInfo.assetId; pAsset.AssetName = pAssetInfo.assetName; pAsset.Type = pAssetInfo.type; pAsset.DepartName = pAssetInfo.departName; pAsset.SiteName = pAssetInfo.siteName; pAsset.UnitName = pAssetInfo.unitName; pAsset.Amount = pAssetInfo.amount; pAsset.Price = pAssetInfo.price; pAsset.Memo = pAssetInfo.memo; pAsset.IsNew=pIsNew; string UserName = SubsonicHelper.GetUserName(); try { pAsset.Save(UserName); } catch(Exception ex) { LogManager.getInstance().getLogger(typeof(AssetInfo)).Error(ex); if(ex.Message.Contains("插入重复键"))//违反了唯一键 { throw new AppException("此对象已经存在");//此处等待优化可以从唯一约束中直接取出提示来,如果没有的话,默认为原始的出错提示 } throw new AppException("保存失败"); } pAssetInfo.assetId = pAsset.AssetId; //如果缓存存在,更新缓存 if (CachedEntityCommander.IsTypeRegistered(typeof(AssetInfo))) { ResetCache(); } }
/// <summary> /// 批量装载 /// </summary> internal static void LoadFromDALPatch(List< AssetInfo> pList, AssetCollection pCollection) { foreach (Asset asset in pCollection) { AssetInfo assetInfo = new AssetInfo(); LoadFromDAL(assetInfo, asset ); pList.Add(assetInfo); } }
//从后台获取数据 internal static void LoadFromDAL(AssetInfo pAssetInfo, Asset pAsset) { pAssetInfo.assetId = pAsset.AssetId; pAssetInfo.assetName = pAsset.AssetName; pAssetInfo.type = pAsset.Type; pAssetInfo.departName = pAsset.DepartName; pAssetInfo.siteName = pAsset.SiteName; pAssetInfo.unitName = pAsset.UnitName; pAssetInfo.amount = pAsset.Amount; pAssetInfo.price = pAsset.Price; pAssetInfo.memo = pAsset.Memo; pAssetInfo.Loaded=true; }
/// <summary> /// 复制为另一个对象 /// </summary> /// <param name="pIndustryTypeInfoTo"></param> public void CopyTo(AssetInfo pAssetInfoTo) { Copy(this, pAssetInfoTo); }
/// <summary> /// 获得分页列表,无论是否是缓存实体都从数据库直接拿取数据 /// </summary> /// <param name="pPageIndex">页数</param> /// <param name="pPageSize">每页列表</param> /// <param name="pOrderBy">排序</param> /// <param name="pSortExpression">排序字段</param> /// <param name="pRecordCount">列表行数</param> /// <returns>数据分页</returns> public static List<AssetInfo> GetPagedList(int pPageIndex,int pPageSize,SortDirection pOrderBy,string pSortExpression,out int pRecordCount) { if(pPageIndex<=1) pPageIndex=1; List< AssetInfo> list = new List< AssetInfo>(); Query q = Asset .CreateQuery(); q.PageIndex = pPageIndex; q.PageSize = pPageSize; q.ORDER_BY(pSortExpression,pOrderBy.ToString()); AssetCollection collection=new AssetCollection(); collection.LoadAndCloseReader(q.ExecuteReader()); foreach (Asset asset in collection) { AssetInfo assetInfo = new AssetInfo(); LoadFromDAL(assetInfo, asset); list.Add(assetInfo); } pRecordCount=q.GetRecordCount(); return list; }
/// <summary> /// 获得数据列表 /// </summary> /// <returns></returns> public static List<AssetInfo> GetList() { string cacheKey = GetCacheKey(); //本实体已经注册成缓存实体,并且缓存存在的时候,直接从缓存取 if (CachedEntityCommander.IsTypeRegistered(typeof(AssetInfo)) && CachedEntityCommander.GetCache(cacheKey) != null) { return CachedEntityCommander.GetCache(cacheKey) as List< AssetInfo>; } else { List< AssetInfo> list =new List< AssetInfo>(); AssetCollection collection=new AssetCollection(); Query qry = new Query(Asset.Schema); collection.LoadAndCloseReader(qry.ExecuteReader()); foreach(Asset asset in collection) { AssetInfo assetInfo= new AssetInfo(); LoadFromDAL(assetInfo,asset); list.Add(assetInfo); } //生成缓存 if (CachedEntityCommander.IsTypeRegistered(typeof(AssetInfo))) { CachedEntityCommander.SetCache(cacheKey, list); } return list; } }
/// <summary> /// 复制一个对象,采用硬编码的方式,避免反射的低效 /// </summary> /// <param name="pIndustryTypeInfoFrom"></param> /// <param name="pIndustryTypeInfoTo"></param> public static void Copy(AssetInfo pAssetInfoFrom, AssetInfo pAssetInfoTo) { pAssetInfoTo.AssetId = pAssetInfoFrom.assetId; pAssetInfoTo.AssetName = pAssetInfoFrom.assetName; pAssetInfoTo.Type = pAssetInfoFrom.type; pAssetInfoTo.DepartName = pAssetInfoFrom.departName; pAssetInfoTo.SiteName = pAssetInfoFrom.siteName; pAssetInfoTo.UnitName = pAssetInfoFrom.unitName; pAssetInfoTo.Amount = pAssetInfoFrom.amount; pAssetInfoTo.Price = pAssetInfoFrom.price; pAssetInfoTo.Memo = pAssetInfoFrom.memo; pAssetInfoTo.Loaded=pAssetInfoFrom.Loaded; }