【整理】Asp.Net代码整理,不断完善中
1.获取存储过程中输出参数的值,以下程序不完善,重在演示获取存储过程输出参数,程序代码如下:
//定义数据库连接
SqlConnection conn=new SqlConnection();
SqlCommand cmd = new SqlCommand();
//定义存储过程输出参数
SqlParameter parameter = new SqlParameter("@ret", SqlDbType.NVarChar, 50);
parameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parameter);
//执行命令
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//获取输出参数的值
int ret = Convert.ToInt32(parameter.Value);
SqlConnection conn=new SqlConnection();
SqlCommand cmd = new SqlCommand();
//定义存储过程输出参数
SqlParameter parameter = new SqlParameter("@ret", SqlDbType.NVarChar, 50);
parameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parameter);
//执行命令
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//获取输出参数的值
int ret = Convert.ToInt32(parameter.Value);
注意:只能用用ExecuteNonQuery()方法,对其他方法无效。
2.可空类型"??"关键字的用法
int? x = null;
Response.Write(x ?? 0);
上面代码打印出来的是0Response.Write(x ?? 0);
int? x = 4;
Response.Write(x ?? 0);
Response.Write(x ?? 0);
上面代码打印出来的是4
总结:"??"操作符的作用,针对可空类型变量,若值为null,则赋操作符右边的值;否则,赋左边的值
3.页面输出缓存
VaryByParam:通过HTTP GET 或HTTP POST中的指定的参数的值来更新缓存,具体代码
VaryByControl:根据指定的控件的有效值来更新缓存
页面输出缓存文件依赖:页面缓存可以依赖于一个或多个文件,当这些文件当中至少一个发生改变时,缓存将被释放的同时更新缓存
聚合缓存依赖
3.页面输出缓存
//最简单的一种,下面的Duration和VaryByParam属性是必须的
<%@ OutputCache Duration="10" VaryByParam="none"%>
<%@ OutputCache Duration="10" VaryByParam="none"%>
//只使用.cs文件来设置页面输出缓存
this.lbShowTime.Text = DateTime.Now.ToString();
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
this.lbShowTime.Text = DateTime.Now.ToString();
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
//通过web.config设置页面输出缓存
//web.config中的配置
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="webcastcache" duration="5" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
//页面文件
<%@ OutputCache CacheProfile="webcastcache" %>
//web.config中的配置
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="webcastcache" duration="5" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
//页面文件
<%@ OutputCache CacheProfile="webcastcache" %>
VaryByParam:通过HTTP GET 或HTTP POST中的指定的参数的值来更新缓存,具体代码
//HTTP GET参数,页面文件
<%@ OutputCache Duration="60" VaryByParam="city" %>
//.cs文件如下
string queryStr = Request.QueryString["city"];
if (queryStr == null)
{
queryStr = "";
}
switch (queryStr.ToLower())
{
case "shanghai":
Response.Write("welcome shanghai!");
break;
case "beijing":
Response.Write("welcome beijing");
break;
default:
Response.Write("No Params");
break;
}
<%@ OutputCache Duration="60" VaryByParam="city" %>
//.cs文件如下
string queryStr = Request.QueryString["city"];
if (queryStr == null)
{
queryStr = "";
}
switch (queryStr.ToLower())
{
case "shanghai":
Response.Write("welcome shanghai!");
break;
case "beijing":
Response.Write("welcome beijing");
break;
default:
Response.Write("No Params");
break;
}
//HTTP POST方式,页面文件
<%@ OutputCache Duration="60" VaryByParam="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
<%@ OutputCache Duration="60" VaryByParam="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
VaryByControl:根据指定的控件的有效值来更新缓存
//页面文件如下
<%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
<%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="TextBox1"%>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
//.cs文件
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(TextBox1.Text);
}
页面输出缓存文件依赖:页面缓存可以依赖于一个或多个文件,当这些文件当中至少一个发生改变时,缓存将被释放的同时更新缓存
//aspx
<asp:Label ID="lbShowTime" runat="server"></asp:Label>
//aspx.cs页面
this.lbShowTime.Text = "CacheByFileDency:" + DateTime.Now.ToString();
//为缓存添加文件依赖
string fileDepencyPath = Server.MapPath("TextFile.txt");
Response.AddFileDependency(fileDepencyPath);
//为缓存添加多文件依赖
string fileDepencyPath1 = Server.MapPath("TextFile.txt");
string fileDepencyPath2 = Server.MapPath("XMLFile.xml");
string[] fileDepencies = new string[] { fileDepencyPath1,fileDepencyPath2};
Response.AddFileDependencies(fileDepencies);
//设置页面输出缓存
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
<asp:Label ID="lbShowTime" runat="server"></asp:Label>
//aspx.cs页面
this.lbShowTime.Text = "CacheByFileDency:" + DateTime.Now.ToString();
//为缓存添加文件依赖
string fileDepencyPath = Server.MapPath("TextFile.txt");
Response.AddFileDependency(fileDepencyPath);
//为缓存添加多文件依赖
string fileDepencyPath1 = Server.MapPath("TextFile.txt");
string fileDepencyPath2 = Server.MapPath("XMLFile.xml");
string[] fileDepencies = new string[] { fileDepencyPath1,fileDepencyPath2};
Response.AddFileDependencies(fileDepencies);
//设置页面输出缓存
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
聚合缓存依赖
Cache["CacheItem1"] = "CacheItem1";
Cache.Insert("CacheItem2", "CacheItem2");
string[] depencies = new string[] { "CacheItem2"};
//监视缓存键的更改
Cache.Insert("CacheItem3", "CacheItem3", new System.Web.Caching.CacheDependency(null, depencies));
//监视XML文件的更改
Cache.Insert("CacheItem4", "CacheItem4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
//聚合缓存依赖的实现
//创建监视XML文件的缓存依赖
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
//创建监视缓存键的依赖
string[] keyDepKey2 = new string[] { "CacheItem1" };
System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDepKey2);
//聚合上面两中依赖,只要两者当中一者变化,则缓存自动更新
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert("CacheItem5", "CacheItem5", aggDep);
Cache.Insert("CacheItem2", "CacheItem2");
string[] depencies = new string[] { "CacheItem2"};
//监视缓存键的更改
Cache.Insert("CacheItem3", "CacheItem3", new System.Web.Caching.CacheDependency(null, depencies));
//监视XML文件的更改
Cache.Insert("CacheItem4", "CacheItem4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
//聚合缓存依赖的实现
//创建监视XML文件的缓存依赖
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
//创建监视缓存键的依赖
string[] keyDepKey2 = new string[] { "CacheItem1" };
System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDepKey2);
//聚合上面两中依赖,只要两者当中一者变化,则缓存自动更新
System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert("CacheItem5", "CacheItem5", aggDep);
Substitution的使用:此控件允许在缓存的页面来实现部分不缓存的功能,第一种实现方式
//aspx页面文件
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" />
//aspx.cs代码文件
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDateTime));
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" />
//aspx.cs代码文件
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDateTime));
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}
第二种实现方式
//aspx页面文件
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" MethodName="GetCurrentDateTime" />
//aspx.cs代码文件如下
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}
<%@ OutputCache Duration="60" VaryByParam="none" %>
<asp:Label ID="lbShowTime" runat="server"></asp:Label><br />
<asp:Substitution ID="SubDateTime" runat="server" MethodName="GetCurrentDateTime" />
//aspx.cs代码文件如下
protected void Page_Load(object sender, EventArgs e)
{
lbShowTime.Text = DateTime.Now.ToString();
}
public static string GetCurrentDateTime(HttpContext context)
{
return DateTime.Now.ToString();
}
4.将当前日期换算成当年的周次
/// <summary>
/// 查询当前日期是当年第几周,整型数
/// </summary>
/// <param name="currentDate">当前日期</param>
/// <returns>Integer类型</returns>
public static int GetCurrentWeek(DateTime currentDate)
{
try
{
DateTime firstDate = new DateTime(currentDate.Year, 1, 1);
int firstWeek = Convert.ToInt32(firstDate.DayOfWeek);
firstWeek = firstWeek.Equals(0) ? 7 : firstWeek;
int currentWeek = Convert.ToInt32(currentDate.DayOfWeek);
currentWeek = currentWeek.Equals(0) ? 7 : currentWeek;
int currentDays = Convert.ToInt32(currentDate.DayOfYear);
int n = (currentDays - 8 + firstWeek - currentWeek) / 7 + 2;
return n;
}
catch (Exception)
{
return 0;
}
}
/// 查询当前日期是当年第几周,整型数
/// </summary>
/// <param name="currentDate">当前日期</param>
/// <returns>Integer类型</returns>
public static int GetCurrentWeek(DateTime currentDate)
{
try
{
DateTime firstDate = new DateTime(currentDate.Year, 1, 1);
int firstWeek = Convert.ToInt32(firstDate.DayOfWeek);
firstWeek = firstWeek.Equals(0) ? 7 : firstWeek;
int currentWeek = Convert.ToInt32(currentDate.DayOfWeek);
currentWeek = currentWeek.Equals(0) ? 7 : currentWeek;
int currentDays = Convert.ToInt32(currentDate.DayOfYear);
int n = (currentDays - 8 + firstWeek - currentWeek) / 7 + 2;
return n;
}
catch (Exception)
{
return 0;
}
}
使用.Net内置函数实现同样功能
/// <summary>
/// 查询当前日期是当年第几周,整型数
/// </summary>
/// <param name="currentDate">当前日期</param>
/// <param name="cultureInfo">区域性</param>
/// <returns>Integer类型</returns>
public static int GetCurrentWeek(DateTime currentDate,string cultureInfo)
{
try
{
System.Globalization.CultureInfo myCultureInfo = new System.Globalization.CultureInfo(cultureInfo);
System.Globalization.Calendar myCalendar = myCultureInfo.Calendar;
System.Globalization.CalendarWeekRule myCalendarWeekRule = myCultureInfo.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCultureInfo.DateTimeFormat.FirstDayOfWeek;
int n = myCalendar.GetWeekOfYear(currentDate, myCalendarWeekRule, myFirstDOW);
return n;
}
catch (Exception)
{
return 0;
}
}
/// 查询当前日期是当年第几周,整型数
/// </summary>
/// <param name="currentDate">当前日期</param>
/// <param name="cultureInfo">区域性</param>
/// <returns>Integer类型</returns>
public static int GetCurrentWeek(DateTime currentDate,string cultureInfo)
{
try
{
System.Globalization.CultureInfo myCultureInfo = new System.Globalization.CultureInfo(cultureInfo);
System.Globalization.Calendar myCalendar = myCultureInfo.Calendar;
System.Globalization.CalendarWeekRule myCalendarWeekRule = myCultureInfo.DateTimeFormat.CalendarWeekRule;
DayOfWeek myFirstDOW = myCultureInfo.DateTimeFormat.FirstDayOfWeek;
int n = myCalendar.GetWeekOfYear(currentDate, myCalendarWeekRule, myFirstDOW);
return n;
}
catch (Exception)
{
return 0;
}
}
5.将枚举值转换为文本
using System;
using System.Collections.Generic;
using System.Text;
namespace EmployeeOA.Entities
{
public class enumState
{
public enum JobKind
{
紧急,
普通
}
public enum ProcessStatus
{
未阅读,
处理中,
已完成
}
public enum Status
{
未查看,
已查看
}
public enum CustomerStatus
{
未交费,
免费邀请
}
}
}
using System.Collections.Generic;
using System.Text;
namespace EmployeeOA.Entities
{
public class enumState
{
public enum JobKind
{
紧急,
普通
}
public enum ProcessStatus
{
未阅读,
处理中,
已完成
}
public enum Status
{
未查看,
已查看
}
public enum CustomerStatus
{
未交费,
免费邀请
}
}
}
下面为处理代码
public string turn(string value)
{
return ((enumState.CustomerStatus)System.Convert.ToInt32(value)).ToString();
}
{
return ((enumState.CustomerStatus)System.Convert.ToInt32(value)).ToString();
}