天天看點

.NET随記【你懂的】

.NET随記.txt放在桌面好久了,程式設計過程中記錄些雜七雜八的東西,分享給大家希望有所幫助。

  1. goto 常用于 switch語句中
  2. 字元串相加用 StringBuilder的Append()方法性能好
  3. str.Trim(',') 清除字元串後的“,”
  4. str.ToString("參數") 可以生成一定的格式
  5. 字元串分隔符截取:str.Split(new char[]{','})
  6. 輸出21個A的簡單做法:string str=new string('A',21)
  7. 字元串轉化整數:Int32.TryParse() 性能更好
  8. <div id="sysBar" style="cursor:pointer;"></ div>    層内顯示手型
  9. windows 服務:若要确定如何啟動服務,請單擊 ServiceInstaller 元件并将 StartType 屬性設定為适當的值。
    Manual 服務安裝後,必須手動啟動
    Automatic 每次計算機重新啟動時,服務都會自動啟動
    Disabled 服務無法啟動
  10. windows服務安裝與解除安裝(指令)

        安裝:轉到:C:\Windows\Microsoft.NET\Framework\v4.0.30319 目錄下,執行InstallUtil.exe MyService.exe指令

        解除安裝:轉到:C:\Windows\Microsoft.NET\Framework\v4.0.30319 目錄下,執行InstallUtil.exe MyService.exe /u指令

  11. Windows服務添加安裝項目

        1,将寫好的windows服務切換到設計視圖,右鍵-添加安裝程式

        2,切換到新生成的ProjectInstaller.cs設計視圖,找到serviceProcessInstaller1對Account屬性設定為 LocalSystem,對serviceInstaller1的ServiceName屬性設定為Server1(服務的名字),StartType屬 性設定為Automatic(系統啟動的時候自動啟動服務)

        3,建立一個新的安裝項目ServerSetup(我們為剛才那個服務建立一個安裝項目)

        4,右鍵-添加-項目輸出-主輸出-選擇Service1-确定

        5,右鍵-視圖-自定義操作-自定義操作上(安裝)右鍵-添加自定義操作-打開應用程式檔案夾-選擇剛才那個主輸出-确定

        6,右鍵-視圖-自定義操作-自定義操作上(解除安裝)右鍵-添加自定義操作-打開應用程式檔案夾-選擇剛才那個主輸出-确定 -設定(arguments屬性='/u' 作為解除安裝時并解除安裝服務)

        7,重新生成,在安裝項目下的bin或release下,可找到可執行的安裝程式,裡面已經包含了所有需要的類庫等。

        8,輕按兩下安裝程式安裝後,在服務管理器中(我的電腦-右鍵-管理-服務和應用程式-服務)找到Server1服務,啟動服務

  12. DOS檢視端口  netstat -a
  13. Scoket實作不同網絡通信: 設定路由器的端口映射
  14. 連接配接字元串:Data Source=(local);Initial Catalog=webSealTicket;Integrated Security=SSPI;         Data Source=server;Initial Catalog=db;User ID=test;Password=test;
  15. 窗體對象.BringToFront()   将窗體放在前面
  16. asp.net在頁面注入腳本:Page.ClientScript.RegisterStartupScript(this.GetType(), "alert1", "<script>alert('" + nameStr + ",你的密碼錯誤!');</script>");
  17. 正規表達式:Regex myreg = new Regex("");   bool b= myreg.IsMatch("要驗證的内容");
  18. 判斷是否連接配接網絡:System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()     
  19. 網絡程式設計注意防火牆

    E:\SQL2008\MSSQL10.MSSQLSERVER\MSSQL\Backup\

  20. Web Service引用: 
    1     LunwenService.myWebService1 lws = new LunwenService.myWebService1();
    2     message = lws.GetLunWenName(2);
    3     message = lws.GetLunWenDS(3);
    4     Response.Write(message);      
  21. 頁面輸出緩存:<%@ OutputCache Duration="60" VaryByParam="none" %>
  22. 從資料庫中取出圖檔頁面顯示: <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="點選圖檔,打開連接配接"

    ImageUrl='<%# Eval("新聞圖檔","~/upload/News/{0}") %>' />

    或:ImageUrl='<%# String.Format("~/upload/News/{0}",Eval("新聞圖檔")) %>'

  23.  DataBinder類 自動執行類型轉換:<%# DataBinder.Eval(Container.DataItem,"資料庫字段名",{0:n})>、<%# DataBinder.Eval(Container.DataItem, "Time", "{0:yyyy-mm-dd}")%>
  24. 跨線程通路控件:CheckForIllegalCrossThreadCalls = false;
  25. this.InvokeRequired: 判斷調用線程是否與建立窗體的線程為同一線程,若不是,則為true;

    this.Invoke();    在建立窗體的線程上執行委托

  26. ToString()參數:

    12345.ToString("n"); 生成 12,345.00

        12345.ToString("C"); 生成 ¥12,345.00

        12345.ToString("e"); 生成 1.234500e+004

        12345.ToString("f4"); 生成 12345.0000

        12345.ToString("x"); 生成 3039(16進制)

        12345.ToString("p"); 生成 1,234,500.00%

        取中文日期顯示_年月 string strYM=currentTime.ToString("y");

        取中文日期顯示_月日 string strMD=currentTime.ToString("m");

        取目前年月日,格式為:2003-9-23 string strYMD=currentTime.ToString("d");

        取目前時分,格式為:14:24 string strT=currentTime.ToString("t");

        Int32.Parse(變量) Int32.Parse("常量") 字元型轉換 轉為32位數字型 3、 變量.ToString() 字元型轉換 轉為字元串 12345.ToString("n"); //生成 12,345.00

        12345.ToString("C"); //生成 ¥12,345.00

        12345.ToString("e"); //生成 1.234500e+004

        12345.ToString("f4"); //生成 12345.0000

        12345.ToString("x"); //生成 3039

  27. asp.net中<% > 用法:在百分号內 , 如果百分号後面不帶任何符号(冒号、等号、井号) , 即表示要執行一段代碼而已,此處不包含任何輸出資訊;若帶符号,即表示執行此處的代碼,并且将執行後傳回的值綁定(或者顯示)在此處。
  28. asp.net中<%# >和<%= >的差別:綁定時機不同,<%# %>是在控件調用DataBind函數的時候才被确定。
  29. Asp.Net的Web表單(頁面)可分為三種模式:

        1.傳統的内聯代碼(.aspx檔案):含有代碼和使用者接口布局的.aspx 檔案

        2.代碼後置(.aspx和.vb/.cs檔案):含有使用者接口的 .aspx 檔案和含有代碼的.vb/.cs 檔案

        3.經過編譯的代碼後置(.aspx和編譯好的.dll檔案/放入\bin目錄中):含有使用者接口的 .aspx 檔案和含有代碼的.vb/.cs 檔案  

  30. 傳回代碼:<a class="back" href="javascript:history.back(-1);">傳回</a>
  31. C語言:

        1 atof():将字元串轉換為雙精度浮點型值。

        2 atoi():将字元串轉換為整型值。

        3 atol():将字元串轉換為長整型值。

        4 strtod():将字元串轉換為雙精度浮點型值,并報告不能被轉換的所有剩餘數字。

        5 strtol():将字元串轉換為長整值,并報告不能被轉換的所有剩餘數字。

        6 strtoul():将字元串轉換為無符号長整型值,并報告不能被轉換的所有剩餘數字。

  32. 運作輸入“regedit”擷取系統資料庫
  33. Linq to DaaTable:
    var query1=from item in dt.AsEnumerable()                
                            orderby item.Field<int>("Age")                
                            descending
                            select item;//排序
            foreach (var item in query1)
            {
                Console.WriteLine("姓名:{0},性别:{1},年齡:{2}",item.Field<string>("Name"),item.Field<string>("Sex"),item.Field<int>("Age"));
            }         
  34. Linq to XML:using System.Xml.Linq;命名空間中重要的三個類:XElement,XAttribute,XDocument
  35. System.Environment.CurrentDirectory     //目前程式的執行目錄
  36. 常用源碼網站:http://www.codeplex.com/,http://sourceforge.net/,http://www.csdn.net/
  37. windows 剪切闆程式 C:\Windows\System32\clipbrd.exe    對應VS中的Clipboard類
  38. .netFramework參考圖書:《CLR via C#(第三版)》譯者:周靖        《C#本質論(第三版)》譯者:周靖
  39. WinForm安裝程式   System32檔案夾下msiexec.exe   參數為 \x
  40. 全局緩沖區指令:Gacutil
  41. <table cellSpacing="10" cellPadding="10" width="100%" border="0">;cellSpacing:列之間的間距;cellPadding:行之間的間距;border邊框的粗細
  42. jquery簡單用法
    1     <script type="text/javascript">
    2       $(document).ready(function(){
    3           $("#btn1").click(function(){
    4               $("#p1").hide();});
    5     });
    6     </script>      
  43. 元素選擇器

    $("p") 選取 <p> 元素。

    $("p.intro") 選取所有 class="intro" 的 <p> 元素。

    $("p#demo") 選取 id="demo" 的第一個 <p> 元素。

    屬性選擇器

        $("[href]") 選取所有帶有 href 屬性的元素。

        $("[href='#']") 選取所有帶有 href 值等于 "#" 的元素。

        $("[href!='#']") 選取所有帶有 href 值不等于 "#" 的元素。

        $("[href$='.jpg']") 選取所有 href 值以 ".jpg" 結尾的元素。

    CSS 選擇器

        $("p").css("background-color","red"); 把所有 p 元素的背景顔色更改為紅色:

  44. Event 函數 綁定函數至

    $(document).ready(function) 文檔的就緒事件(當 HTML 文檔就緒可用)

    $(selector).click(function) 被選元素的點選事件

    $(selector).dblclick(function) 被選元素的輕按兩下事件

    $(selector).focus(function) 被選元素的獲得焦點事件

    $(selector).mouseover(function) 被選元素的滑鼠懸停事件

  45. jquery效果

    基本:show("slow,function(){};"),hide()

        切換:toggle("slow")-可實作顯示和隐藏

        滑動:slideDown("slow"),slideUp(),slideToggle()-可以實作滑上滑下

        淡入淡出:fadeIn("slow"),fadeOut()

        透明度:fadeTo("slow",0.25)

        自定義:animate({height:400},"slow")-可以多個

  46. JS
    1     document.getElementById("id").style.background="#1112";
    2     document.getElementById("id").style.background="url(img.ipg)";      
  47. this.ddlSheng.DataSource = dsData.Tables[0];

    this.ddlSheng.DataTextField = "Zone";  //指定表中的某列

  48. 正規表達式:(using System.Text.RegularExpressions;)
    1     Regex regex = new Regex(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
    2     if(regex.IsMatch(mail))
    3          return true;      
  49. Sql server遠端連接配接字元串:

        data source=192.168.1.111;initial catalog=BD102010;user id=sa;password=sa;

        server=IP,1433;database=ChildrenERecord;Uid=child;Pwd=child

  50. 存儲過程分頁
    1     create procedure pro_fenye
     2     @pageSize int,@pageNow int
     3     as
     4     begin
     5     Select top @pageSize 字段名清單     from 表名     where id not in(
     6         select top @pageSize*(@pageNow-1) id
     7         from 表名)
     8     end
     9     --@pageSize 每頁的顯示多少條資料
    10     --@pageNow 目前顯示的是第幾頁      
  51. 頁面重置:

        <input name="重置" type="reset" class="submit" value="重置"/>

  52. 資料庫插入資訊傳回最新ID:

        insert into dbo.SeManage_WeixiuQicai(WeixiuID,QicaiID,[Count],Zhuangtai,Explain)

        values(1,'7b53331c-c6fd-4ef0-a028-03bdf2a67b4f',111,'未修','asdasd');select @@IDENTITY

  53. <% %> 中可以寫任何C#代碼

        <%Response.Write("asdasdas"); %>

  54. MSChart圖表控件綁定資料庫
    1     chart1.DataSource=Ds;
    2     chart1.Series["Series 1"].XValueMember = "Name";
    3         chart1.Series["Series 1"].YValueMembers = "Sales";
    4     chart1.DataBind();      
  55. MSChart餅圖綁定
    1     double [] yval = { 2,6,4,5,3};
    2     string [] xval = { "Peter", "Andrew", "Julie", "Mary", "Dave"};
    3     Chart1.Series["Series 1"].Points.DataBindXY(xval,yval);      
  56. 腳本或ActivX輸出中文出現亂碼解決:    Web.config中加入

        <system.web><globalization requestEncoding="gb2312" responseEncoding="gb2312" /></system.web>

  57. MSChart綁定要以統計每個使用者的年銷售曲線,那麼分組統計的字段名應該設定為Name

        Chart1.DataBindCrossTable(myReader,"Name","Year" ,"Sales","Label=Commissions{C}");

  58. 取得檔案的擴充名

        System.IO.Path.GetExtension(string path);

  59. js擷取項目根路徑
    1     function getRootPath(){
     2         //擷取目前網址,如: http://localhost:8083/uimcardprj/share/meun.jsp
     3         var curWwwPath=window.document.location.href;
     4         //擷取主機位址之後的目錄,如: uimcardprj/share/meun.jsp
     5         var pathName=window.document.location.pathname;
     6         var pos=curWwwPath.indexOf(pathName);
     7         //擷取主機位址,如: http://localhost:8083
     8         var localhostPaht=curWwwPath.substring(0,pos);
     9         //擷取帶"/"的項目名,如:/uimcardprj
    10         var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    11         return(localhostPaht+projectName);
    12     }      
  60. js擷取DropDownList的SelectText

        (document.getElementById("dropdownlistid").options[count].innerText == 設定的值

  61. Net系列(ORM):  EntitysCodeGenerate  LINQ TO SQL  Grove  Rungoo.EnterpriseORM  FireCode Creator                      MyGeneration  CodeSmith Pro  CodeAuto   NHibernate
  62. 檢測危險代碼web.config添加:<httpRuntime requestValidationMode="2.0" /><pages validateRequest="false"></pages>
  63. http://abowman.com/google-modules/dog/

作者:田園裡的蟋蟀

微信公衆号:你好架構

出處:http://www.cnblogs.com/xishuai/

公衆号會不定時的分享有關架構的方方面面,包含并不局限于:Microservices(微服務)、Service Mesh(服務網格)、DDD/TDD、Spring Cloud、Dubbo、Service Fabric、Linkerd、Envoy、Istio、Conduit、Kubernetes、Docker、MacOS/Linux、Java、.NET Core/ASP.NET Core、Redis、RabbitMQ、MongoDB、GitLab、CI/CD(持續內建/持續部署)、DevOps等等。

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接。

分享到:

QQ空間

新浪微網誌

騰訊微網誌

微信

更多