2017年7月27日 星期四

C# 找到 呼叫的Function的完整名稱

請參考以下的程式碼
MethodBase method = stackTrace.GetFrame(1).GetMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
Console.WriteLine(className + "." + methodName);


How to find FULL name of calling method C#

C# WPF Textbox只能輸入數字

在引用的View.Xaml畫面裡面加入這行,做事件綁定。

這樣就可以做到只能輸入數字了。
DelayStartTimeTextBox.PreviewTextInput += StaticResourceModel.PreviewTextInput;
    public static class StaticResourceModel
    {
        public static void PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = new System.Text.RegularExpressions.Regex("[^0-9]+").IsMatch(e.Text);
        }
    }

C# 泛型List加入到ObservableCollection

怕以後忘記,先寫起來。

首先先New一個ObservableCollection跟List。

並且在ObservableCollection的()內加入到你要放的List。

這樣就可以把List的資料轉移到ObservableCollection。


public ObservableCollection MyString = new ObservableCollection(new List());

C# ICommand 執行多個 Function 或加入參數

在 obj => 加入大括號,如下程式碼。

public ICommand ResetProgramCommand { get { if (_resetProgramCommand == null) { _resetProgramCommand = new RelayCommand(obj => {_event.ResetProgramEvent() , _event.UpdateProgramEvent()}, null); } return _resetProgramCommand; } }


如果是加入參數就變成以下這樣
string value = string.empty;
string result = "Success";

public ICommand ResetProgramCommand { get { if (_resetProgramCommand == null) { _resetProgramCommand = new RelayCommand(obj => {_event.ResetProgramEvent(),value = result;}, null); } return _resetProgramCommand; } }

C# 如何匯入外部DLL及如何傳值運作

今天被主管問到一個問題,.NET的參考元件順序是什麼? 一時答不出來,也沒有這樣的觀念,所以找了一些資料。

.NET參考元件的順序

1.GAC (C:\Windows\assembly)

2.執行exe檔底下同目錄的DLL檔

3.環境變數內的路徑,去尋找該DLL檔

讓我對.NET的參考元件順序有更深的認識

[.NET] 組件安裝與配置 / Assembly Install and Configuration

C# 如何匯入外部DLL及如何傳值運作

2017年7月24日 星期一

xml for linq 讀取

如果我有以下的XML檔案格式,但是想要取得LanguageInfo的參數,可以用以下的xml for linq的程式碼取得
 











首先宣告XDocument的物件,並給它檔案路徑去讀取,然後再new一個ObservableCollection的string泛型,透過linq的語法去取得attribute的參數,並轉成list在轉型放入到ObservableCollection。
XDocument allLanguage = XDocument.Load($@"{Properties.Settings.Default.ConfigPath}");

name = = new ObservableCollection((from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo").Select(args => args.Attribute("name").Value).ToList());

value = new ObservableCollection((from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo").Select(args => args.Attribute("value").Value).ToList());

如果是要放到一個類別裡面也可以這樣宣告

foreach(XElement element in (from data in allLanguage.Descendants("TaskManager").Descendants("Language") select data).Elements("LanguageInfo"))
{
StaticResourceModel.ComboBoxValue.Add(new ComboBoxValue() {Name= element.Attribute("name").Value , Value = element.Attribute("value").Value});
}

2017年6月20日 星期二

SQL Server 利用Linked server 連結到 MySQL Server

In this article let’s discuss the step by step procedure to create a linked server to MySQL from SQL Server. First, download the MySQL drivers from the location below. Choose the 32 bit or 64 bit versions as per SQL Server and Windows Server platform requirements. http://dev.mysql.com/downloads/connector/odbc/ Next, install the driver using .msi package

Select typical as option



Click on install, and this will install MySQL drivers onto the Windows Server. This needs to be run from the Windows machine that runs your SQL Server instance.



Once the driver is installed we need to make an ODBC entry for the MySQL server. To do this, go to run and type “odbcad32.exe” and hit enter. It will pop up below screen.



Click on Add. It will pop up below screen. Select “MySQL ODBC 5.3 Unicode Driver” and click on Finish.



Once we click on Finish, the below screen will pop up.



Enter the valid MYSQL server details in MySQL Connector/ODBC Data Source Configuration dialog box.



Click on Test, to validate the connection. If we are connecting to MySQL for the first time from this Windows server, we get error message as below.



To validate the host, run below command on the MySQL server. Make sure you are connected to the MySQL server as root or an equivalent user. GRANT ALL ON *.* to fooUser@''; Now click on Test, and the connection will be successfully established.



Click on details to set up a few more parameters. For better performance, I recommend using the below options.





Once the ODBC entry is set, configure MSDASQL in SSMS (SQL Server Management Studio).



Make sure the following four options are checked: Nested queries Level zero only Allow inprocess Supports ‘Like’ Operator For more info visit this link: http://technet.microsoft.com/en-us/library/ms191462%28v=sql.105%29.aspx



Now create the linked server in Management Studio.



Enter the ODBC record details we created in above steps. Make sure we selected "Microsoft OLEDB Provider for ODBC Drivers" as the Provider



Enter the security details for the server. Click on OK. It will create the linked server.

Test the linked server by right clicking and selecting test connection.



To verify the linked server through SQL statements, run below in query window of Management Studio: SELECT TOP 10 * FROM MYSQL...test_table
參考來源:http://www.sqlservercentral.com/articles/Linked+Server/115955/