2017年9月26日 星期二

Func 委派的用法

Func 委派的用法

https://dotblogs.com.tw/brooke/2014/08/02/146142

摘要:Func 委派的用法
一、
1.委派的範例為
using System;

//宣告一個委派型別為ConvertMethod,
//並規範入為string型別,輸出為string型別
delegate string ConvertMethod(string inString);

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      // 將委派型別ConvertMethod的實體去參考UppercaseString方法
      ConvertMethod aConvertMeth = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(aConvertMeth(name));
   }

   private static string UppercaseString(string inputString)
   {
      return inputString.ToUpper();
   }
}

2.不用明確定義委派方法,以Func來簡化程式碼為
using System;

public class GenericFunc
{
    public static void Main()
    {
        // Instantiate delegate to reference UppercaseString method
        //Func<in T, out TResult>
        Func<string, string> aConvertMethod = UppercaseString;
        string name = "Dakota";
        // Use delegate instance to call UppercaseString method
        Console.WriteLine(aConvertMethod(name));
        Console.ReadKey();
    }

    private static string UppercaseString(string inputString)
    {
        return inputString.ToUpper();
    }
}

3.使用匿名方法
using System;

public class Anonymous
{
   public static void Main()
   {
      Func<string, string> convert = delegate(string s){ return s.ToUpper();}; 

      string name = "Dakota";
      Console.WriteLine(convert(name));   
   }
}

4.使用Lambda運算式
using System;

public class Anonymous
{
   public static void Main()
   {
      Func<string, string> convert = s => s.ToUpper(); 

      string name = "Dakota";
      Console.WriteLine(convert(name));   
   }
}

二、
若要參考有一個參數並傳回 void (在 Visual Basic 中是宣告為 Sub 而非 Function) 的方法,
可使用泛型 Action<T> 委派代替。
using System;

public class GenericFunc
{
    public static void Main()
    {

        Action<string> aConvertMethod = UppercaseString;
        string name = "Dakota";

        aConvertMethod(name);
        Console.ReadKey();
    }

    private static void UppercaseString(string inputString)
    {
        Console.WriteLine(inputString.ToUpper());
    }
}

三、
Action<T>泛型的委派型別,也是跟Func一樣,只是回傳值為void。
每個Func委派型別的簽章,傳入的參數會是零到四,而最後一個的型別參數是代表回傳型別。
例如:Func<string,int>,代表傳入型別為string而回型別為int。

2017年9月25日 星期一

Visual Studio 2012 無法啟動IIS Express Web 伺服器

Visual Studio 2012 無法啟動IIS Express Web 伺服器

http://marco.easyusing.com/2012/12/visual-studio-2012-iis-express-web.html

字串_Replace用法

String s = "文字1 - Microsoft word";

//先將空格轉為$,再將"-"刪
s = s.Replace(" ", "$").Replace("-", "");
 
結果
文字1$$Microsoft$word



C#中的IntPtr類型

https://read01.com/zh-tw/DAOJK.html#.WcjooMgjFPY

C#中的IntPtr類型

問:
c#中無法將類型「int」隱式轉換為「System.IntPtr」
這個是我引用了一個api函數時出現的問題,我在聲明中把intptr換成了int還是不可以,這是為什麼呢?要如何處理呢?

答:
您好,C#中的IntPtr類型稱為「平台特定的整數類型」,它們用於本機資源,如窗口句柄。
資源的大小取決於使用的硬體和作業系統,但其大小總是足以包含系統的指針(因此也可以包含資源的名稱)。

所以,在您調用的API函數中一定有類似窗體句柄這樣的參數,那麼當您聲明這個函數時,您應該將它顯式地聲明為IntPtr類型。

例如,在一個C#程序中調用Win32API mciSendString函數控制光碟驅動器,這個函數的函數原型是:

MCIERROR mciSendString
(
LPCTSTR lpszCommand,
LPTSTR lpszReturnString,
UINT cchReturn,
HANDLE hwndCallback
);

首先在C#中聲明這個函數:

[DllImport("winmm.dll")]

private static extern long mciSendString(string a,string b,uint c,IntPtr d);

然後用這樣的方法調用:

mciSendString("set cdaudio door open", null, 0, this.Handle);

您也可以使用IntPtr.Zero將句柄設置為0;

或者使用類型強制轉換:

mciSendString("set cdaudio door open", null, 0, (IntPtr)0 );

或者,使用IntPtr構造函數:

IntPtr a = new IntPtr(2121);

這裡有兩點比較重要:

一是在C#中聲明Win32API時,一定要按照WinAPI的原型來聲明,不要改變它的數據類型;

二是儘量不要過多使用類型強制轉換或構造函數的方式初始化一個IntPtr類型的變量,這樣會使程序變得難於理解並容易出錯。

希望這些信息對您有幫助。



2017年9月24日 星期日

如何使用 CAST 與 CONVERT 格式化日期與時間資料

如何使用 CAST 與 CONVERT 格式化日期與時間資料


http://ithelp.ithome.com.tw/articles/10008820


SSMS--T-SQL偵錯工具教學


http://blog.kkbruce.net/2011/01/ssms-t-sql.html#.WcdyAMgjFPY

2017年9月15日 星期五

asp.net-ExecuteNonQuery-的回傳值

ExecuteNonQuery 方法不會返回任何資料庫的資料, 它只會返回整數值來表示成功或受影響的資料列數目.


  • create

If use ExecuteNonQuery to create or modify database structure, eg. create table, this method returns -1 if success, returns 0 if fail.

ExecuteNonQuery 用來創建或修改資料庫的結構,如建立table,成功回1,失敗回0


  •  INSERT, UPDATE, DELETE

If use ExecuteNonQuery to INSERT, UPDATE, DELETE, this method returns the Number of affected data row, but if fail, it returns 0.

如果用來新增修改刪除,成功它會返回受影響的列數,失敗回0.

[ADO.NET] 為何 / 如何 使用 SQLParameter 物件

[ADO.NET] 為何 / 如何 使用 SQLParameter 物件


在上一個範例 [ADO.NET] 如何 使用 SQLCommand 查詢資料庫https://dotblogs.com.tw/yc421206/archive/2009/06/10/8776.aspx
當中
1.在插入資料的SQL陳述句,若能使用 Parameter 將會提高安全性,若使用者輸入了特別符號,也比較不會出問題;而寫ASP.NET必須更注意安全性的問題。
2.Parameter 可以 (1)檢查參數的型別 (2)檢查資料長度 (3)確保參數為非可執行的SQL命令 ,如下圖。
3.上一個範例的queryString 變數是
queryString = "insert into mytable (myregion,myname) values(N' " + region + "',N'" + name + "' )";


改為以下
string queryString = "insert into mytable (myregion,myname) values(@myregion,@myname)";


主要是使用"@參數名稱",來代替原本的變數。


4.使用 SQLCommand.Parameters 屬性是引用了 SqlParameterCollection 類別,所以也是集合的一種,如果忘了集合是啥米小請看 [C#.NET][VB.NET] 一般 / 泛型 Generic Collection 集合型別介紹https://dotblogs.com.tw/yc421206/archive/2009/01/25/6941.aspx


5.Parameter 用法很簡單,如下範例:"@myregion", 為變數、textBox1.Text 為對應變數的數值
cmd.Parameters.AddWithValue("@myregion", textBox1.Text);


如何使用SQLParameter 物件


以下範例需引用 System.Data.SqlClient 命名空間,以下為部份範例


1.引用SqlConnection物件連接資料庫
using (SqlConnection cn = new SqlConnection(cs))


2.開啟資料庫
cn.Open();


3.引用SqlCommand物件
using (SqlCommand cmd = new SqlCommand(queryString, cn))


4.加入 SQLParameters(以下執行結果均為相同)


  4.1 使用 SQLCommand.Parameters.Add 方法
      cmd.Parameters.Add("@myregion", SqlDbType.NVarChar);
      cmd.Parameters["@myregion"].Value = textBox1.Text;


  4.2 使用 SQLCommand.Parameters.AddWithValue 方法
     cmd.Parameters.AddWithValue("@myregion", textBox1.Text);


    
  4.3 使用 SQLCommand.Parameters.Add 方法加入 SQLParameters 類別
     cmd.Parameters.Add(new SqlParameter("@myregion", textBox1.Text));


5.執行SQL語法
cmd.ExecuteNonQuery();



WPF聊天室应用(ASP.NET Core SignalR)

  WPF聊天室应用(ASP.NET Core SignalR) https://www.bilibili.com/video/BV1Q741187Si?p=2 https://www.bilibili.com/video/BV1UV411e75T?from=search...