如何处理异常 如输入100超出范围的异常
private void btnCal_Click(object sender, System.EventArgs e)
{
try
{
int nInput = int.Parse(tbInput.Text);
long nResult=1;
for(int i=1;i<=nInput;i++)
checked{nResult *= i;}//处理异常
lbResult.Text = nResult.ToString();
}
catch(OverflowException)//溢出错误异常处理
{
throw new Exception("输入的数字太大!");
}
catch(FormatException)//输入字符格式错误异常处理
{
Response.Write("要输入整数!");
}
catch(Exception ee)
{
Response.Write("发生错误,信息为:"+ee.Message);
}
}
private void WebForm1_Error(object sender, System.EventArgs e)
{//页面异常处理将异常写入Windows系统日志中
string strMessage = Server.GetLastError().Message;
//Response.Write(strMessage);
Server.ClearError();
//以下把信息写入windows日志
//要把aspnet用户添加到管理员组中,以便有写注册表权限
if(!EventLog.SourceExists("mySource"))
EventLog.CreateEventSource("mySource","myLog");
EventLog Event = new EventLog();
Event.Source = "mySource";
Event.WriteEntry(strMessage,EventLogEntryType.Warning);
//EventLog.Delete("myLog");
throw new Exception("这里处理不了,转到Global.asax处理!");
}
Global.asax
protected void Application_Error(Object sender, EventArgs e)
{
//把错误信息发送到作者
string strPageUrl = Request.Path;
Exception ErrorInfo =Server.GetLastError();
//Server.ClearError();
string strMessage = "Url:" + strPageUrl + "</br>";
strMessage = strMessage + " Error: ";
strMessage = strMessage + ErrorInfo.ToString() + "</br>";
MailMessage Mymessage = new MailMessage();
Mymessage.To = "gloomyboyo@126.com'";
Mymessage.From = gloomyboyo@126.com;
Mymessage.Subject = "ASP.NET Error";
Mymessage.BodyFormat = MailFormat.Text;
Mymessage.Body = strMessage;
SmtpMail.Send(Mymessage);
}
Web.config
<customErrors
mode="On"
defaultRedirect="CustomError.aspx"
/>
比较使用和不使用连接池所发费的时间
private void btnOK_Click(object sender, System.EventArgs e)
{
string strConUnusePool = "server=(local);database=mydatabase;uid=sa;pwd=111;pooling=false";//不使用
string strConusePool = "server=(local);database=mydatabase;uid=sa;pwd=111;pooling=true";
int nConNum = int.Parse(tbNum.Text);
//计算不使用连接池创建连接的时间
DateTime dtStart = DateTime.Now;
for(int i=1;i<=nConNum;i++)
{
SqlConnection con = new SqlConnection(strConUnusePool);
con.Open();
con.Close();
}
DateTime dtEnd = DateTime.Now;
TimeSpan ts = dtEnd-dtStart;
lbUnuse.Text = ts.Milliseconds.ToString();
//计算使用连接池的时间
dtStart = DateTime.Now;
for(int i=1;i<=nConNum;i++)
{
SqlConnection con = new SqlConnection(strConusePool);
con.Open();
con.Close();
}
dtEnd = DateTime.Now;
ts = dtEnd-dtStart;
lbUse.Text = ts.Milliseconds.ToString();
}
<%# DataBinder.Eval(Container.DataItem,"字段名")%>
效率更高
<@%((DataRowView)Container.DataItem)["字段名"]%>
关于缓存10秒刷新
<%@ OutPutCache Duration="10" VaryByParam="None"%>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
lbTime.Text = DateTime.Now.ToLongTimeString();
}
XML文件
<NewDataSet>
<User>
<CustName>Jack</CustName>
<CustIdno>1</CustIdno>
<CustCard>142123333321</CustCard>
</User>
</NewDataSet>
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
LoadData();
}
private void LoadData()
{
//当缓存对象DataCache5有效时,从缓存中读出客户信息;无效时,从文件读出信息
DataView dv1 = (DataView)Cache["DataCache5"];
if(dv1==null)
{
DataSet ds;
FileStream fs;
StreamReader sr;
ds = new DataSet();
fs = new FileStream(Server.MapPath("custom1.xml"),FileMode.Open,FileAccess.Read);
sr = new StreamReader(fs);
ds.ReadXml(sr);
fs.Close();
dv1=new DataView(ds.Tables[0]);
Cache.Insert("DataCache5",dv1,new CacheDependency(Server.MapPath("custom1.xml")));
lblMsg.Text="数据从文件中读出...";
}
else
lblMsg.Text="数据从缓存中读出...";
//绑定到DataGrid1对象
dgShow.DataSource = dv1;
dgShow.DataBind();
}
private void AddBtn_Click(object sender, System.EventArgs e)
{
//增加一个客户信息到文件中
FileStream FS;
StreamReader Reader;
DataSet DS;
DataRow dr1;
TextWriter tw1;
if(!Page.IsValid)
lblMsg.Text ="还有域未曾填充...";
else
{
DS=new DataSet();
FS=new FileStream(Server.MapPath("custom1.xml"),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
Reader=new StreamReader(FS);
DS.ReadXml(Reader);
FS.Close();
dr1=DS.Tables[0].NewRow();
dr1["CustName"]=txtName.Text;
dr1["CustIdno"]=txtIdno.Text;
dr1["CustCard"]=txtCard.Text;
DS.Tables[0].Rows.Add(dr1);
FS=new FileStream(Server.MapPath("custom1.xml"),FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
tw1=new StreamWriter(FS);
tw1=TextWriter.Synchronized(tw1);
DS.WriteXml(tw1);
tw1.Close();
LoadData();
}
}
private void btnFresh_Click(object sender, System.EventArgs e)
{
LoadData();
}