public void SizeOfDateTime() { long n = GC.GetTotalMemory(true); DateTime date = DateTime.Now; //88byte Activity activity = new Activity(); //72byte long m = GC.GetTotalMemory(true); Console.WriteLine("{0}-{1}={2}", m, n, m - n); //88byte }
public void GetSize() { //获取activity对象的占用的内存字节大小 GC.Collect(0); var activity = new Activity(); long n = GC.GetTotalMemory(true); activity = null; GC.Collect(0); long m = GC.GetTotalMemory(true); Console.WriteLine("{0}-{1}={2}", n, m, n - m);//72字节 //acitvity中有int*4+string*4+datetime*4+bool*1 //72-(16+1+32) }
public void GetSize3() { //必须加上 [StructLayout(LayoutKind.Sequential)] 才能调用该方法 object activity = new Activity(); Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(activity)); }
public void GetSize2() { object activity = new Activity(); Console.WriteLine(GetSize(ref activity)); }
/// <summary> /// 更新一条数据 /// </summary> public static int Update(Activity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update ExamDB.dbo.tb_Activity set "); strSql.Append("activity_content=@activity_content,"); strSql.Append("activity_welcometext=@activity_welcometext,"); strSql.Append("activity_companyintrotext=@activity_companyintrotext,"); strSql.Append("activity_logo=@activity_logo"); strSql.Append(" where activity_id=@activity_id"); SqlParameter[] parameters = { new SqlParameter("@activity_content", SqlDbType.NVarChar,255), new SqlParameter("@activity_welcometext", SqlDbType.NVarChar,255), new SqlParameter("@activity_companyintrotext", SqlDbType.NVarChar,255), new SqlParameter("@activity_logo", SqlDbType.NVarChar,255), new SqlParameter("@activity_id", SqlDbType.Int,4)}; parameters[0].Value = model.Activity_content; parameters[1].Value = model.Activity_welcometext; parameters[2].Value = model.Activity_companyintrotext; parameters[3].Value = model.Activity_logo; parameters[4].Value = model.Activity_id; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); }
/// <summary> /// 得到一个对象实体 /// </summary> public static Activity GetModel(int activityid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 * from ExamDB.dbo.tb_Activity"); strSql.Append(" where activity_id=@activityid"); SqlParameter[] parameters = { new SqlParameter("@activityid", SqlDbType.Int,4)}; parameters[0].Value = @activityid; Activity model = new Activity(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { model.Activity_id = int.Parse(ds.Tables[0].Rows[0]["activity_id"].ToString()); model.Activity_enterpriseuid = int.Parse(ds.Tables[0].Rows[0]["activity_enterpriseuid"].ToString()); model.Activity_positionid = int.Parse(ds.Tables[0].Rows[0]["activity_positionid"].ToString()); model.Activity_name = ds.Tables[0].Rows[0]["activity_name"].ToString(); model.Activity_content = ds.Tables[0].Rows[0]["activity_content"].ToString(); model.Activity_status = int.Parse(ds.Tables[0].Rows[0]["activity_status"].ToString()); model.Activity_logo = ds.Tables[0].Rows[0]["activity_logo"].ToString(); model.Activity_viewreport = int.Parse(ds.Tables[0].Rows[0]["activity_viewreport"].ToString()); model.Activity_welcometext = ds.Tables[0].Rows[0]["activity_welcometext"].ToString(); model.Activity_companyintrotext = ds.Tables[0].Rows[0]["activity_companyintrotext"].ToString(); if (ds.Tables[0].Rows[0]["activity_createtime"].ToString() != "") { model.Activity_createtime = DateTime.Parse(ds.Tables[0].Rows[0]["activity_createtime"].ToString()); } if (ds.Tables[0].Rows[0]["activity_holdtime"].ToString() != "") { model.Activity_holdtime = DateTime.Parse(ds.Tables[0].Rows[0]["activity_holdtime"].ToString()); } model.Activity_enterpriseuid = int.Parse(ds.Tables[0].Rows[0]["activity_enterpriseuid"].ToString()); model.Activity_positionid = int.Parse(ds.Tables[0].Rows[0]["activity_positionid"].ToString()); return model; } else { return null; } }
/// <summary> /// 增加一条数据 /// </summary> public static int Add(Activity model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into ExamDB.dbo.tb_Activity("); strSql.Append(@"activity_enterpriseuid, activity_positionid, activity_name, activity_content, activity_status, activity_logo, activity_viewreport, activity_welcometext, activity_createtime, activity_holdtime, activity_companyintrotext)"); strSql.Append(" values ("); strSql.Append(@"@activity_enterpriseuid, @activity_positionid, @activity_name, @activity_content, @activity_status, @activity_logo, @activity_viewreport, @activity_welcometext, @activity_createtime, @activity_holdtime, @activity_companyintrotext)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@activity_enterpriseuid", SqlDbType.Int,4), new SqlParameter("@activity_positionid", SqlDbType.Int,4), new SqlParameter("@activity_name", SqlDbType.NVarChar,255), new SqlParameter("@activity_content", SqlDbType.NVarChar,255), new SqlParameter("@activity_status", SqlDbType.Int ,4), new SqlParameter("@activity_logo", SqlDbType.NVarChar,255), new SqlParameter("@activity_viewreport", SqlDbType.Int ,4), new SqlParameter("@activity_welcometext", SqlDbType.NVarChar,255), new SqlParameter("@activity_createtime",SqlDbType.DateTime), new SqlParameter("@activity_holdtime",SqlDbType.DateTime), new SqlParameter("@activity_companyintrotext", SqlDbType.NVarChar,255) }; parameters[0].Value = model.Activity_enterpriseuid; parameters[1].Value = model.Activity_positionid; parameters[2].Value = model.Activity_name; parameters[3].Value = model.Activity_content; parameters[4].Value = model.Activity_status; parameters[5].Value = model.Activity_logo; parameters[6].Value = model.Activity_viewreport; parameters[7].Value = model.Activity_welcometext; parameters[8].Value = model.Activity_createtime; parameters[9].Value = DBNull.Value; parameters[10].Value = model.Activity_companyintrotext; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return -1; } else { return Convert.ToInt32(obj); } }