/// <summary>/// DataSet导出Excel/// </summary>/// <param name="arrTitle">列标题,若为null,则直接取dataset列标题</param>/// <param name="ds">要导出的DataSet</param>/// <param name="fileName">Excel文件名,不需要传入扩展名</param>protected void CreateExcel(string[] arrTitle, DataSet ds, string fileName){StringBuilder strb = new StringBuilder();strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");strb.Append(" <head> <meta http-equiv=’Content-Type' content=’text/html; charset=gb2312′>");strb.Append(" <style>");strb.Append(".xl26");strb.Append(" {mso-style-parent:style0;");strb.Append(" font-family:\"Times New Roman\", serif;");strb.Append(" mso-font-charset:0;");strb.Append(" mso-number-format:\"@\";}");strb.Append(" </style>");strb.Append(" <xml>");strb.Append(" <x:ExcelWorkbook>");strb.Append(" <x:ExcelWorksheets>");strb.Append(" <x:ExcelWorksheet>");strb.Append(" <x:Name>Sheet1 </x:Name>");strb.Append(" <x:WorksheetOptions>");strb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");strb.Append(" <x:Selected/>");strb.Append(" <x:Panes>");strb.Append(" <x:Pane>");strb.Append(" <x:Number>3 </x:Number>");strb.Append(" <x:ActiveCol>1 </x:ActiveCol>");strb.Append(" </x:Pane>");strb.Append(" </x:Panes>");strb.Append(" <x:ProtectContents>False </x:ProtectContents>");strb.Append(" <x:ProtectObjects>False </x:ProtectObjects>");strb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");strb.Append(" </x:WorksheetOptions>");strb.Append(" </x:ExcelWorksheet>");strb.Append(" <x:WindowHeight>6750 </x:WindowHeight>");strb.Append(" <x:WindowWidth>10620 </x:WindowWidth>");strb.Append(" <x:WindowTopX>480 </x:WindowTopX>");strb.Append(" <x:WindowTopY>75 </x:WindowTopY>");strb.Append(" <x:ProtectStructure>False </x:ProtectStructure>");strb.Append(" <x:ProtectWindows>False </x:ProtectWindows>");strb.Append(" </x:ExcelWorkbook>");strb.Append(" </xml>");strb.Append("");strb.Append(" </head> <body> <table align=\"center\" style=’border-collapse:collapse;table-layout:fixed'> <tr>"); if (ds.Tables.Count > 0){//写列标题 if […]
View Detailsusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.OleDb;using System.Xml;using System.Xml.Xsl;using System.IO; namespace Amei.Data{ partial class DataSet { #region ExcelHelper #region 读取Excel public System.Data.DataSet ReadExcel(string path) { System.Data.DataSet ds = new System.Data.DataSet(); string extension = System.IO.Path.GetExtension(path.ToLower()); string connString = string.Empty; if (extension == ".xls") { connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } else if (extension == ".xlsx") { connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } string query = "SELECT * FROM [Sheet1$]"; try { OleDbConnection conn = new OleDbConnection(); OleDbCommand cmd […]
View Detailspublic void repeater1_ItemCreated(object sender, RepeaterItemEventArgs e) { DataRowView row = (DataRowView)e.Item.DataItem; string url = "id="+row["zyid"].ToString(); string text = row["职业名称"].ToString(); string cat_num = row["数量"].ToString(); HyperLink link = new HyperLink(); link.NavigateUrl = "upPage.aspx?" + url; link.Text = text; e.Item.Controls.Add(link); […]
View Details一、准备知识点: 1.对文件操作,先引用两个命名空间:using System.IO;(操作文件)、using Sysetem.Text;(操作文本) 2.创建文本文件:(1)创建文件名和文件内容(相当于新建文本文档)(2)创建StreamWriter对象,创建一个某某格式的文件(3)将内容写入数据流WriteLine (4)关闭StreamWrite对象.Close() 方法一:直接创建一个StreamWriter对象 string filename = TextBox1.Text string filecontent= TextBox2.Text StreamWriter sw =File.CreatText(Server.MapPath("~/txt/"+ filename +".txt")); sw.WriteLine(filecontent); sw.Close(); Response.Write("<script>alert('已经成功新建了一个’+ filename +'.txt,并添加了数据')</script>") 方法二:在创建StreamWriter对象之前先创建一个FileStream(文件流)对象,并在最后关闭它。 string filename = TextBox1.Text string filecontent= TextBox2.Text FileStream fs = new FileStream(Server.MapPath("~/txt/"+ filename + ".txt")),FileMode.Create,FileAccess.Write); //文件流fs的路径、文件打开方式:创建和写操作 StreamWriter sw = new StreamWriter(fs,Encoding.Default); //文件流fs和浏览器默认编码类型 sw.WriteLine(filecontent); sw.Close(); fs.Close(); Response.Write("<script>alert('成功新建了一个’+ filename +'.txt,并添加了数据')</script>") 3.读取文本文本: 方法一:直接使用File.ReadAllText(文件路径,编码方式) TextBox1.Text=File.ReadAllText(Server.MapPath("~/txt/**.txt"),Encoding.Default); 方法二:使用StreamReader对象以File.OpenText(文件路径)读取文件数据以及使用StreamBuilder对象的Append属性来添加读取文件的数据(UTF-8规范读取的数据) StreamReader sr = File.OpenText(Server.MapPath("~/txt/**.txt")); StreamBuilder sb = new StreamBuilder(); while(!sr.EndOfStream) //如果数据不到最后一行,继续添加(循环语句) { sb.Append(sr.ReadLine() +"<br>");} sr.Close(); 方法三:使用StreamBuilder对象的Append属性添加从StreamReader对象那里读取的数据流,与前一种方法不同的是,这次用到了FileStream的File.Open方法取代了File.OpenText(文件路径)的方法(GB2312规范读取数据) StreamBuilder sb = new StreamBuilder(); FileStream fs = File.Open(Server.MapPath("~/txt/**.txt"),FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs,Encoding.Default); […]
View Details一.设置web.config相关选项先启用窗体身份验证和默认登陆页,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">authentication </span><span style="color: #ff0000">mode</span><span style="color: #0000ff">="Forms"</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">forms </span><span style="color: #ff0000">loginUrl</span><span style="color: #0000ff">="default.aspx"</span><span style="color: #0000ff">></</span><span style="color: #800000">forms</span><span style="color: #0000ff">></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">authentication</span><span style="color: #0000ff">></span></div> |
设置网站可以匿名访问,如下
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">allow </span><span style="color: #ff0000">users</span><span style="color: #0000ff">="*"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span></div> |
然后设置跟目录下的admin目录拒绝匿名登陆,如下。注意这个小节在System.Web小节下面。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">location </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="admin"</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">system.web</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"><</span><span style="color: #800000">deny </span><span style="color: #ff0000">users</span><span style="color: #0000ff">="?"</span><span style="color: #0000ff">></</span><span style="color: #800000">deny</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"></</span><span style="color: #800000">authorization</span><span style="color: #0000ff">></span><span style="color: #000000"><br /> </span><span style="color: #0000ff"></</span><span style="color: #800000">system.web</span><span style="color: #0000ff">></span><span style="color: #000000"><br /></span><span style="color: #0000ff"></</span><span style="color: #800000">location</span><span style="color: #0000ff">></span></div> |
把http请求和发送的编码设置成GB2312,否则在取查询字符串的时候会有问题,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">globalization </span><span style="color: #ff0000">requestEncoding</span><span style="color: #0000ff">="gb2312"</span><span style="color: #ff0000"> responseEncoding</span><span style="color: #0000ff">="gb2312"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
设置session超时时间为1分钟,并启用cookieless,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">sessionState </span><span style="color: #ff0000">mode</span><span style="color: #0000ff">="InProc"</span><span style="color: #ff0000"> cookieless</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> timeout</span><span style="color: #0000ff">="1"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
为了启用页面跟踪,我们先启用每一页的trace,以便我们方便的调试,如下。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff"><</span><span style="color: #800000">trace </span><span style="color: #ff0000">enabled</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> requestLimit</span><span style="color: #0000ff">="1000"</span><span style="color: #ff0000"> pageOutput</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> traceMode</span><span style="color: #0000ff">="SortByTime"</span><span style="color: #ff0000"> localOnly</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">/></span></div> |
二.设置Global.asax文件 处理Application_Start方法,实例化一个哈希表,然后保存在Cache里
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> Application_Start(Object sender, EventArgs e)<br />{<br /> Hashtable h</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> Hashtable();<br /> Context.Cache.Insert(</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">,h);<br />}</span></div> |
在Session_End方法里调用LogoutCache()方法,方法源码如下
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> 清除Cache里当前的用户,主要在Global.asax的Session_End方法和用户注销的方法里调用<br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> LogoutCache()<br />{<br /> Hashtable h</span><span style="color: #000000">=</span><span style="color: #000000">(Hashtable)Context.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(h</span><span style="color: #000000">!=</span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(h[Session.SessionID]</span><span style="color: #000000">!=</span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> h.Remove(Session.SessionID);<br /> Context.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">h;<br /> }<br />}</span></div> |
三.设置相关的登陆和注销代码 登陆前调用PreventRepeatLogin()方法,这个方法可以防止用户重复登陆,如果上次用户登陆超时大于1分钟,也就是关闭了所有admin目录下的页面达到60秒以上,就认为上次登陆的用户超时,你就可以登陆了,如果不超过60秒,就会生成一个自定义异常。在Cache["online"]里保存了一个哈西表,哈西表的key是当前登陆用户的SessionID,而Value是一个ArrayList,这个ArrayList有两个元素,第一个是用户登陆的名字第二个元素是用户登陆的时间,然后在每个admin目录下的页刷新页面的时候会更新当前登陆用户的登陆时间,而只admin目录下有一个页打开着,即使不手工向服务器发送请求,也会自动发送一个请求更新登陆时间,下面我在页面基类里写了一个函数来做到这一点,其实这会增加服务器的负担,但在一定情况下也是一个可行的办法.
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> 防止用户重复登陆,在用户将要身份验证前使用<br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /></span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="name"></span><span style="color: #008000">要验证的用户名字</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> PreventRepeatLogin(</span><span style="color: #0000ff">string</span><span style="color: #000000"> name)<br />{<br /> Hashtable h </span><span style="color: #000000">=</span><span style="color: #000000"> (Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (h </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> IDictionaryEnumerator e1 </span><span style="color: #000000">=</span><span style="color: #000000"> h.GetEnumerator();<br /> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> flag </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">while</span><span style="color: #000000"> (e1.MoveNext())<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> ((</span><span style="color: #0000ff">string</span><span style="color: #000000">)((ArrayList)e1.Value)[</span><span style="color: #800080">0</span><span style="color: #000000">] </span><span style="color: #000000">==</span><span style="color: #000000"> name)<br /> {<br /> flag </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (flag)<br /> {<br /> TimeSpan ts </span><span style="color: #000000">=</span><span style="color: #000000"> System.DateTime.Now.Subtract(Convert.ToDateTime(((ArrayList)e1.Value)[</span><span style="color: #800080">1</span><span style="color: #000000">]));<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (ts.TotalSeconds </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #800080">60</span><span style="color: #000000">)<br /> </span><span style="color: #0000ff">throw</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> oa.cls.MyException(</span><span style="color: #800000">"</span><span style="color: #800000">对不起,你输入的账户正在被使用中,如果你是这个账户的真正主人,请在下次登陆时及时的更改你的密码,因为你的密码极有可能被盗窃了!</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> h.Remove(e1.Key);<br /> }<br /> }<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> {<br /> h </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Hashtable();<br /> }<br /> ArrayList al </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> ArrayList();<br /> al.Add(name);<br /> al.Add(System.DateTime.Now);<br /> h[Session.SessionID] </span><span style="color: #000000">=</span><span style="color: #000000"> al;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> Context.Cache.Insert(</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">, h);<br /> }<br /> </span><span style="color: #0000ff">else</span><span style="color: #000000"><br /> Cache[</span><span style="color: #800000">"</span><span style="color: #800000">Online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> h;<br />}</span></div> |
用户注销的时候调用上面提到LogoutCache()方法 四.设置admin目录下的的所有页面的基类
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">using</span><span style="color: #000000"> System;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI.WebControls;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Web.UI.HtmlControls;<br /></span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections;<br /></span><span style="color: #0000ff">namespace</span><span style="color: #000000"> oa.cls<br />{<br /> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> MyBasePage : System.Web.UI.Page<br /> {<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 获取本页是否在受保护目录,我这里整个程序在OA的虚拟目录下,受保护的目录是admin目录<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">bool</span><span style="color: #000000"> IsAdminDir<br /> {<br /> </span><span style="color: #0000ff">get</span><span style="color: #000000"><br /> {<br /> </span><span style="color: #0000ff">return</span><span style="color: #000000"> Request.FilePath.IndexOf(</span><span style="color: #800000">"</span><span style="color: #800000">/oa/admin</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 防止session超时,如果超时就注销身份验证并提示和转向到网站默认页<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> PreventSessionTimeout()<br /> {<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsAdminDir) </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000">(Session[</span><span style="color: #800000">"</span><span style="color: #800000">User_Name</span><span style="color: #800000">"</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #0000ff">null</span><span style="color: #000000">&&</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsAdminDir)<br /> { <br /> System.Web.Security.FormsAuthentication.SignOut();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.Alert(</span><span style="color: #800000">"</span><span style="color: #800000">登陆超时</span><span style="color: #800000">"</span><span style="color: #000000">,Request.ApplicationPath)<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 每次刷新本页面的时候更新Cache里的登陆时间选项,在下面的OnInit方法里调用.<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> UpdateCacheTime()<br /> {<br /> Hashtable h </span><span style="color: #000000">=</span><span style="color: #000000"> (Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">];<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (h </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> ((ArrayList)h[Session.SessionID])[</span><span style="color: #800080">1</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> DateTime.Now;<br /> }<br /> Cache[</span><span style="color: #800000">"</span><span style="color: #800000">Online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">=</span><span style="color: #000000"> h;<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 在跟踪里输出一个HashTable的所有元素,在下面的OnInit方法里调用.以便方便的观察缓存数据<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="myList"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> TraceValues(Hashtable myList)<br /> {<br /> IDictionaryEnumerator myEnumerator </span><span style="color: #000000">=</span><span style="color: #000000"> myList.GetEnumerator();<br /> </span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800080">0</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">while</span><span style="color: #000000"> (myEnumerator.MoveNext())<br /> {<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineSessionID</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, myEnumerator.Key.ToString());<br /> ArrayList al </span><span style="color: #000000">=</span><span style="color: #000000"> (ArrayList)myEnumerator.Value;<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineName</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, al[</span><span style="color: #800080">0</span><span style="color: #000000">].ToString());<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">onlineTime</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> i, al[</span><span style="color: #800080">1</span><span style="color: #000000">].ToString());<br /> TimeSpan ts </span><span style="color: #000000">=</span><span style="color: #000000"> System.DateTime.Now.Subtract(Convert.ToDateTime(al[</span><span style="color: #800080">1</span><span style="color: #000000">].ToString()));<br /> Context.Trace.Write(</span><span style="color: #800000">"</span><span style="color: #800000">当前的时间和此登陆时间间隔的秒数</span><span style="color: #800000">"</span><span style="color: #000000">, ts.TotalSeconds.ToString());<br /> i</span><span style="color: #000000">++</span><span style="color: #000000">;<br /> }<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 弹出信息并返回到指定页<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="msg"></span><span style="color: #008000">弹出的消息</span><span style="color: #808080"></param></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="url"></span><span style="color: #008000">指定转向的页面</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> Alert(</span><span style="color: #0000ff">string</span><span style="color: #000000"> msg, </span><span style="color: #0000ff">string</span><span style="color: #000000"> url)<br /> {<br /> </span><span style="color: #0000ff">string</span><span style="color: #000000"> scriptString </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"><script language=JavaScript>alert(\</span><span style="color: #800000">""</span><span style="color: #800000"> + msg + </span><span style="color: #800000">"</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000">);location.href=\</span><span style="color: #800000">""</span><span style="color: #800000"> + url + </span><span style="color: #800000">"</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000"></script></span><span style="color: #800000">"</span><span style="color: #000000">;<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsStartupScriptRegistered(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">))<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.RegisterStartupScript(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">, scriptString);<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 为了防止常时间不刷新页面造成会话超时,这里写一段脚本,每隔一分钟向本页发送一个请求以维持会话不被超时,这里用的是xmlhttp的无刷新请求<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 这个方法也在下面的OnInit方法里调用<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> XmlReLoad()<br /> {<br /> System.Text.StringBuilder htmlstr </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> System.Text.StringBuilder();<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"><SCRIPT LANGUAGE=\</span><span style="color: #800000">"</span><span style="color: #000000">JavaScript\</span><span style="color: #800000">"</span><span style="color: #800000">></span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">function GetMessage(){</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> var xh=new ActiveXObject(\</span><span style="color: #800000">"</span><span style="color: #000000">Microsoft.XMLHTTP\</span><span style="color: #800000">"</span><span style="color: #800000">);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> xh.open(\</span><span style="color: #800000">"</span><span style="color: #0000ff">get</span><span style="color: #000000">\</span><span style="color: #800000">"</span><span style="color: #800000">,window.location,false);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> xh.send();</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"> window.setTimeout(\</span><span style="color: #800000">"</span><span style="color: #000000">GetMessage()\</span><span style="color: #800000">"</span><span style="color: #800000">,60000);</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">}</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000">window.onload=GetMessage();</span><span style="color: #800000">"</span><span style="color: #000000">);<br /> htmlstr.Append(</span><span style="color: #800000">"</span><span style="color: #800000"></SCRIPT> </span><span style="color: #800000">"</span><span style="color: #000000">);<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #0000ff">this</span><span style="color: #000000">.IsStartupScriptRegistered(</span><span style="color: #800000">"</span><span style="color: #800000">xmlreload</span><span style="color: #800000">"</span><span style="color: #000000">))<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.RegisterStartupScript(</span><span style="color: #800000">"</span><span style="color: #800000">alert</span><span style="color: #800000">"</span><span style="color: #000000">, htmlstr.ToString());<br /> }<br /> </span><span style="color: #0000ff">override</span><span style="color: #000000"> </span><span style="color: #0000ff">protected</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> OnInit(EventArgs e)<br /> {<br /> </span><span style="color: #0000ff">base</span><span style="color: #000000">.OnInit(e);<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.PreventSessionTimeout();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.UpdateCacheTime();<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.XmlReLoad();<br /> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #0000ff">this</span><span style="color: #000000">.Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">] </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">)<br /> {<br /> </span><span style="color: #0000ff">this</span><span style="color: #000000">.TraceValues((System.Collections.Hashtable)Cache[</span><span style="color: #800000">"</span><span style="color: #800000">online</span><span style="color: #800000">"</span><span style="color: #000000">]);<br /> }<br /> }<br /> }<br />}</span></div> |
五.写一个自定义异常类首先要在跟目录下写一个错误显示页面ShowErr.aspx,这个页面根据传递过来的查询字符串msg的值,在一个Label上显示错误信息。
1 |
<div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">using</span><span style="color: #000000"> System;<br /></span><span style="color: #0000ff">namespace</span><span style="color: #000000"> oa.cls<br />{<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> MyException 的摘要说明。<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> MyException : ApplicationException<br /> {<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException()<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">()<br /> {<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="ErrMessage"></span><span style="color: #008000">异常消息</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException(</span><span style="color: #0000ff">string</span><span style="color: #000000"> Message)<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">(Message)<br /> {<br /> System.Web.HttpContext.Current.Response.Redirect(</span><span style="color: #800000">"</span><span style="color: #800000">~/ShowErr.aspx?msg=</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> Message);<br /> }<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> 构造函数<br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="Message"></span><span style="color: #008000">异常消息</span><span style="color: #808080"></param></span><span style="color: #008000"><br /> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="InnerException"></span><span style="color: #008000">引起该异常的异常类</span><span style="color: #808080"></param></span><span style="color: #808080"><br /></span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> MyException(</span><span style="color: #0000ff">string</span><span style="color: #000000"> Message, Exception InnerException)<br /> : </span><span style="color: #0000ff">base</span><span style="color: #000000">(Message, InnerException)<br /> {<br /> }<br /> }<br />}</span></div> |
转自:http://www.cnblogs.com/hcbin/archive/2010/04/02/1702885.html
View DetailsFCKeditor 很强大也很讨人喜欢,唯一的缺点就是不支持文件和文件夹的删除,很是遗憾。这篇文章就介绍怎样为 FCKeditor 增加删除功能(基于 C# 的版本)。 FCKeditor 官方网站:http://www.fckeditor.net本文所针对版本:FCKeditor: 2.6.4,FCKeditor.Net: 2.6.3。 1. 用 Visual Studio 2005/2008 打开项目 FCKeditor.Net 2.6.3,打开文件“FileBrowser/Connector.cs”,为 class Connector 增加如下两个成员函数:
1 |
private void DelFile( XmlNode connectorNode, string resourceType, string currentFolder ) { HttpContext hc = HttpContext.Current; string file = hc.Server.MapPath(hc.Request["FileUrl"]); if (System.IO.File.Exists(file)) System.IO.File.Delete(file); else hc.Response.Write(@"<error number=""1"" originaldescription=""unable to locate file"">"); } private void DelFolder( XmlNode connectorNode, string resourceType, string currentFolder ) { HttpContext hc = HttpContext.Current; string folder = hc.Server.MapPath(hc.Request["FolderName"]); if (System.IO.Directory.Exists(folder)) System.IO.Directory.Delete(folder, true); else hc.Response.Write(@"<error number=""2"" originaldescription=""unable to locate folder"">"); } |
2. 在文件“Connector.cs”中找到 OnLoad 函数,在 switch 部分增加以下红色代码:
1 |
// Execute the required command. switch( sCommand ) { case "GetFolders" : this.GetFolders( oConnectorNode, sResourceType, sCurrentFolder ); break; case "GetFoldersAndFiles" : this.GetFolders( oConnectorNode, sResourceType, sCurrentFolder ); this.GetFiles( oConnectorNode, sResourceType, sCurrentFolder ); break; case "CreateFolder": this.CreateFolder(oConnectorNode, sResourceType, sCurrentFolder); break; <span><font color="#ff0000" face="Courier New">case "DelFile": this.DelFile(oConnectorNode, sResourceType, sCurrentFolder); break; case "DelFolder": this.DelFolder(oConnectorNode, sResourceType, sCurrentFolder); break;</font></span> } |
3. 编译 FCKeditor.net 并关闭该项目。将生成的 FredCK.FCKeditorV2.dll 拷贝出来以备后用。 4. 建立 C# 测试项目,并在其中部署 FCKeditor 2.6.4(使用第 3 步生成的 FredCK.FCKeditorV2.dll)。精简及部署的详细步骤非本文重点,不再重复。 5. 打开“fckeditor/editor/filemanager/browser/default/frmresourceslist.htm”,修改以下两个函数
1 |
oListManager.GetFolderRowHtml = function( folderName, folderPath<font face="Courier New"><span><font color="#ff0000">, folderUrl</font></span> ) { // Build the link to view the folder. var sLink = '<a href="#" onclick="OpenFolder(\'' + ProtectPath(folderPath) + '\');return false;">'; return '<tr>' + '<td width="16">' + sLink + '<img alt="" src="images/Folder.gif" width="16" height="16" border="0"><\/a>' + '<\/td><td nowrap colspan="2"> ' + sLink + folderName + '<\/a>' + '<\/td><span><font color="#ff0000"><td align="right"><a href="#" onclick="DelFolder(\''+folderName+'\',\''+ ProtectPath(folderUrl) + '\');return false;">删除</a></td<span><font color="#ff0000">></font></span><</font></span>\/tr>'; } oListManager.GetFileRowHtml = function( fileName, fileUrl, fileSize ) { // Build the link to view the folder. var sLink = '<a href="#" onclick="OpenFile(\'' + ProtectPath(fileUrl) + '\');return false;">' ; // Get the file icon. va sIcon = oIcons.GetIcon( fileName ) <span><font color="#ff0000"><td align="right"><a href="#" onclick="DelFile(\''+fileName+'\',\'' + ProtectPath(fileUrl) + '\');return false;">删除</a></td></font></span></font><\/tr>'; } |
6. 继续修改文件“frmresourceslist.htm”,在 OpenFile 函数后面增加以下两个函数:
1 |
function DelFile( fileName, fileUrl ) { if (confirm('您确定要删除文件“' + fileName + '”吗?')) oConnector.SendCommand("DelFile", "FileUrl=" + escape(fileUrl), Refresh); } function DelFolder( folderName, folderPath ) { if (confirm('您确定要删除文件夹“' + folderName + '”和里面的所有文件吗?')) oConnector.SendCommand("DelFolder", "FolderName=" + escape(folderPath + folderName), Refresh); } |
7. 继续修改文件“frmresourceslist.htm”,找到 GetFoldersAndFilesCallBack 函数中的下面这行,增加红色部分的代码:
1 |
oHtml.Append( oListManager.GetFolderRowHtml( sFolderName, sCurrentFolderPath + sFolderName + "/"<span><font color="#ff0000" face="Courier New">, <span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New"><span><font color="#ff0000" face="Courier New">sCurrentFolderUrl</font></span> </font></span></font></span></font></span></font></span></font></span></font></span></font></span></font></span>) ); |
至此,删除功能增加完毕,见下图: 转自:http://blog.sina.com.cn/s/blog_4dfeeb6f0100ed5j.html
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Collections; namespace DBUtility { /// <summary> /// 数据库操作类 /// </summary> public abstract class SqlHelper { // 数据库连接 public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString; // 用哈希表缓存参数 private static readonly Hashtable ParmCache = Hashtable.Synchronized(new Hashtable()); /// <summary> /// 执行一个SQL语句(连接字符串) /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>返回命令影响的行数</returns> public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); using (var conn = new SqlConnection(connectionString)) { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); var val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 执行一个SQL语句(连接对象) /// </summary> /// <param name="connection">一个已经存在的连接对象</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>返回命令影响的行数</returns> public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); var val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> /// 执行一个SQL语句(事务) /// </summary> /// <param name="trans">一个已经存在的事务</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>an int representing the number of rows affected by the command</returns> public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); var val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// <summary> /// 执行一个返回DataReader的SQL语句 /// </summary> /// <param name="connectionString">一个有效的连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>返回一个DataReader对象</returns> public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); var conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try { PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch { conn.Close(); throw; } } /// <summary> /// 返回DataSet /// </summary> /// <param name="connectionString"></param> /// <param name="cmdText"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static DataSet ExecuteDataSet(string connectionString, string cmdText, params SqlParameter[] cmdParms) { using (var connection = new SqlConnection(connectionString)) { var cmd = new SqlCommand(); PrepareCommand(cmd,connection,null,CommandType.Text,cmdText,cmdParms); using (var da = new SqlDataAdapter(cmd)) { var ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (SqlException ex) { throw new Exception(ex.Message); } return ds; } } } /// <summary> /// 根据SQL语句返回DataSet /// </summary> /// <param name="cmdText"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static DataSet Query(string cmdText, params SqlParameter[] cmdParms) { using (var connection = new SqlConnection(ConnectionStringLocalTransaction)) { var cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, CommandType.Text, cmdText, cmdParms); using (var da = new SqlDataAdapter(cmd)) { var ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (SqlException ex) { throw new Exception(ex.Message); } return ds; } } } /// <summary> /// 执行返回一个对象的SQL语句(连接字符串) /// </summary> /// <param name="connectionString">连接字符串</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>返回一个object对象,对象中只包含一列。</returns> public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); using (var connection = new SqlConnection(connectionString)) { PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); var val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } } /// <summary> /// 执行返回一个对象的SQL语句(连接对象) /// </summary> /// <param name="connection">一个已经存在的连接对象</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="commandParameters">参数集</param> /// <returns>返回一个object对象,对象中只包含一列。</returns> public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) { var cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); var val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } /// <summary> /// 添加参数集到缓存 /// </summary> /// <param name="cacheKey">缓存的名称</param> /// <param name="commandParameters">要缓存的参数集</param> public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) { ParmCache[cacheKey] = commandParameters; } /// <summary> /// 从缓存中获取参数集 /// </summary> /// <param name="cacheKey">key used to lookup parameters</param> /// <returns>Cached SqlParamters array</returns> public static SqlParameter[] GetCachedParameters(string cacheKey) { var cachedParms = (SqlParameter[])ParmCache[cacheKey]; if (cachedParms == null) return null; var clonedParms = new SqlParameter[cachedParms.Length]; var j = cachedParms.Length; for (var i = 0; i < j; i++) clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms; } /// <summary> /// 为执行准备命令对象 /// </summary> /// <param name="cmd">command 对象</param> /// <param name="conn">连接对象</param> /// <param name="trans">事务对象</param> /// <param name="cmdType">命令类型</param> /// <param name="cmdText">命令字符串</param> /// <param name="cmdParms">参数集</param> private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, IEnumerable<SqlParameter> cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms == null) return; foreach (var parm in cmdParms) cmd.Parameters.Add(parm); } /// <summary> /// 是否存在记录 /// </summary> /// <param name="strSql"></param> /// <returns></returns> public static bool Exists(string strSql) { var obj = ExecuteScalar(ConnectionStringLocalTransaction, CommandType.Text, strSql); int cmdresult; if ((Equals(obj, null)) || (Equals(obj, DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } return cmdresult != 0; } /// <summary> /// 是否存在记录 /// </summary> /// <param name="strSql"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static bool Exists(string strSql, params SqlParameter[] cmdParms) { var obj = ExecuteScalar(ConnectionStringLocalTransaction,CommandType.Text,strSql, cmdParms); int cmdresult; if ((Equals(obj, null)) || (Equals(obj, DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } return cmdresult != 0; } } } |
View Details
解决: 初次碰到这个问题,试过很多方法,比如:停止索引服务,重装IIS,重装.Net Framework,删除Temporary ASP.NET Files目录再重赋权限,甚至重装系统,问题依旧;还有一点很奇怪,我重装系统前赋予了Temporary ASP.NET Files文件夹asp.net,network service和everyone用户的最大权限,但是我格式化重装系统后,Temporary ASP.NET Files目录的权限设置居然跟重装系统之前一样,不知是不是NTFS文件系统的问题,网上搜了一下,很多人碰到这个问题想到的都是给Temporary ASP.NET Files目录赋权限,我开始也是这样,最后在微软的网站搜到了这个问题的解决方法,其实很简单,只要在windows/temp权限设置里面把Network service(如果是win2000则是asp.net用户)的权限加上就行。 具体解决方法请参照:http://support.microsoft.com/default.aspx?scid=kb;en-us;825791
View Details刚开始学习asp.net,常常遇到一些奇怪的问题,最近做asp.net的毕业设计,把其中的系统做好后在本机上能够正常运行,但是上传到服务器中就出现了下列问题提示: System.Security.SecurityException: That assembly does not allow partially trusted callers., 从百度查了很多资料,回答的不是不沾边,就是凭自己感觉答复。从google搜了下,前几页几乎全是英文页面,看的头大,感觉有的解释有那个道理。最后综合想了下终于明白了, 1)服务器端:将客户端的访问权限设置为full trusted callers,可以正常使用服务器资源,但是咱们对服务器不能要求有什么改动,要求了提供商也是没反应 2)自身系统:我发现系统出现这个问题的地方都是因为用到了分页控件AspNetPager.dll,于是,想到有网友提议: AssemblyInfo.cs文件中加如下内容 using System.Security; [assembly: AllowPartiallyTrustedCallers()] 我想,是啊,突然间又想这个分页控件的新版本已经把这个问题解决了吧,这样我就不必自己修改AssemblyInfo.cs文件再重新编译生成AspNetPager.dll了。从网上又下了个AspNetPagerV7.02的,然后覆盖网站空间中原来那个,再打开自己的网页,发现问题解决了! —————————————————————————————————————————————————————————————— 以上信息转自:http://blog.csdn.net/gzq521/archive/2009/05/13/4178093.aspx 经本人验证 AssemblyInfo.cs文件 的方法行不通。还是下载了个最新的AspNetPager,问题解决,谢谢gzq521。
View Details关于asp.net 循环输出多个随机数相同时的解决方法 我们在使用Random生成随机数时经常会出现一种误区,下面我们就此介绍一下: 其中用于产生随机数的函数原型为:Rnd(x),该函数用以产生一个介于0~1之间的单精度随机数。x是产生随机数的种子。如果要从(min, max)这个范围内随机抽取一个数,此时需要用到公式:随机数 = (max – min) * Rnd(x) + min。一般的生成随机数方法: Random ran=new Random(); int temp=ran.Next(100);生成100以内的一个随机数;这是没有什么问题的 但是我们经常用到for循环生成多个随机数,这时候可能就会出现问题,如果你对随机数原理不是很了解的话,很可能会出现一个误区如: for(int i=0;i<100;i++) { Random ran=new Random(); int temp=ran.Next(100); Responsed.Write(temp.ToString()); } 这时候你会发现一个问题:生成的所有的随机数都是一样的,不管你ran.Next的值怎么设置都是一样的,为是为什么呢,这就是我们要说的一种误区,其实正确的写法是这样的: Random ran=new Random(); for(int i=0;i<100;i++) { int temp=ran.Next(100); Responsed.Write(temp.ToString()); } 看看是不是得到了自己想要的东西。呵呵 转自:http://hi.baidu.com/arqq1230/blog/item/770fb1fab1b7ed9f58ee901c.html
View Details