2019年11月21日 星期四

[pdf] [C#] 網頁匯出PDF - PuppeteerSharp

最近遇到需要將頁面轉PDF

這裡推薦一個套件,真的滿厲害的

比起自己用itext刻快多了~


參考範例 :

特別說明,如果要讓pdf完整套用跟畫面一致,請記得設定參數唷
new PdfOptions() { PrintBackground = true, PreferCSSPageSize = true }

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
{
    Headless = true 
}))
{
    //參考頁面 https://www.puppeteersharp.com/api/PuppeteerSharp.ScreenshotOptions.html
    using (var page = await browser.NewPageAsync())
    {
        
        var loginPath = $"https://google.com";
        await page.GoToAsync(loginPath);
        await page.EmulateMediaTypeAsync(PuppeteerSharp.Media.MediaType.Screen);
        //透過SetViewport控制視窗大小決定抓圖尺寸
        await page.SetViewportAsync(new ViewPortOptions
        {
            Width = 1024,
            Height = 768
        });
 
        var outputFile = $"test.pdf";
        await page.PdfAsync(outputFile, new PdfOptions() { PrintBackground = true, PreferCSSPageSize = true });
 
    }
}



參考

黑暗blog :https://blog.darkthread.net/blog/headless-chrome/

Api說明 : https://www.puppeteersharp.com/api/PuppeteerSharp

2019年10月25日 星期五

[deploy] 遠端發佈 error - 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”

錯誤訊息 : 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”


解法 : 發現結果是沒有安裝 .net 4.6, 再安裝即可

1.進windows功能 啟用 .net 4.6

2.管理者權限進入 cmd

32位元:C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

64位位元:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i


ref :https://www.cnblogs.com/mrma/p/3529859.html

2019年10月18日 星期五

[sql] 切開同一欄位用逗點隔開的資訊


遇到一個狀況,資料庫顯示為

| CODE |   FK    |
-----------------------
|  A  |   1,2,3 |
我需要轉如下才能關聯....

| CODE |  FK   |
-----------------------
|  A  |     1 |
|  A  |     2 |
|  A  |     3 |

解法 :
參考 : https://stackoverflow.com/questions/13527537/sql-query-to-split-column-data-into-rows

先建立sql function
create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1))       
returns @temptable TABLE (items varchar(MAX))       
as       
begin      
    declare @idx int       
    declare @slice varchar(8000)       

    select @idx = 1       
        if len(@String)<1 or @String is null  return       

    while @idx!= 0       
    begin       
        set @idx = charindex(@Delimiter,@String)       
        if @idx!=0       
            set @slice = left(@String,@idx - 1)       
        else       
            set @slice = @String       

        if(len(@slice)>0)  
            insert into @temptable(Items) values(@slice)       

        set @String = right(@String,len(@String) - @idx)       
        if len(@String) = 0 break       
    end   
return 
end;

然後下以下sql 句即可
select t1.Code, s.items FK
from [table] t1
outer apply dbo.Split(t1.FK, ',') s

outer apply : 說明參考 https://ithelp.ithome.com.tw/articles/10195219

2019年10月16日 星期三

deadlock 相關蒐集

開始之前先補充一下,查詢專案sql使用狀況,可以用SSMS的活動監控器







接下來就可以觀察最近使用次數最多或是最耗時的sql, 進而觀察table的使用情況


---


最近上完非同步後,認識後deadlock, 多發生於呼叫非同步方法後,結果尚未回傳,, 就又用同步的方式去要結果導致 (ex. .Result)

這次在專案log發現deadlock, 檢查後發現,卻是sql 查詢時, datareader 查詢較久導致, 所以找一些相關文獻如下, 目前推測的狀況應該就是sql查詢使用到平行查詢,才導致deadlock情況

做個紀錄

目前看到幾個解決方式 :

1.修正SQL
2.不使用平行執行計畫。
3.maxdop=1。
4.用try catch(Error 1205 is a deadlock)預防處理死結。
等等


參考內容 :
[SQL Server][Deadlock]Intra-Query Parallel Thread Deadlocks初體驗
https://dotblogs.com.tw/stanley14/2017/07/22/intra-queryparallelthreaddeadlocks

[SQL Server][DeakLock]觀察死結的工具(四)擴充事件(Extended events)
https://dotblogs.com.tw/stanley14/2017/01/28/191015


限定資料表僅能用 Table Lock
https://byronhu.wordpress.com/2011/05/18/%e9%99%90%e5%ae%9a%e8%b3%87%e6%96%99%e8%a1%a8%e5%83%85%e8%83%bd%e7%94%a8-table-lock/

[SQL SERVER]內部平行查詢死結特性
https://dotblogs.com.tw/ricochen/2016/08/25/191618

2019年9月25日 星期三

[angularJs] popup 使用 : angular-ui

使用 : angular-ui

預覽參考 : https://plnkr.co/edit/?p=preview

1. 加入ui-bootstrap-tpls-2.5.0.min.js
2. module include ui.bootstrap
3. 加入$uibModal
4. 建立fn  openMyModalView
5. 建立template controller

<script type="text/ng-template" id="DetailView.html">
    <div class="modal-header">
        <h3 class="modal-title" id="modal-title">I'm a modal!</h3>
    </div>
    <div class="modal-body" id="modal-body">
        <ul>
            <li ng-repeat="item in ctrl.items">
                <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ ctrl.selected.item }}</b>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ctrl.ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="ctrl.cancel()">Cancel</button>
    </div>
</script>
    <script>
    var DefaultApp = angular.module("Default", ['ngTable','ui.bootstrap']);
    DefaultApp.controller('RegistrationController',
        ['$scope', '$http', 'NgTableParams', '$uibModal', function ($scope, $http, NgTableParams, $uibModal) {
            console.log("test12")
 
            var self = this;
            self.List = @Html.Raw(ViewBag.Registration);
            console.log(self.List);
 
            //初始化
            self.Init = function()
            {
                self.tableParams = new NgTableParams({ count: 50 }, { dataset: self.List });
            }
            self.Init();
 
             //方法
            // ref : https://angular-ui.github.io/bootstrap
            self.openMyModalView = function (item) {
                var modalInstance = $uibModal.open({
                    templateUrl: 'DetailView.html',
                    controller: 'UserDetailCtrl as ctrl',
                    resolve: {
                        item: function () {
                            return item;
                        }
                    }
                });
 
                modalInstance.result.then(function (obj) {
                    console.log(obj)
                }, function () {
                    $log.info('modal-component dismissed at: ' + new Date());
                });
            };
 
            self.SaveHandWrite = function () {
                console.log(self.Edit);
            }
    }]
);
    //popup 事件
    angular.module('Default').controller('UserDetailCtrl', function ($uibModalInstance, item) {
        console.log(item);
        var $ctrl = this;
 
        item.Name = "test";
 
        $ctrl.ok = function () {
            $uibModalInstance.close(item);
        };
 
        $ctrl.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
    </script>
 
 

2019年9月12日 星期四

[C#] SYSTEM.IO 常用到的檔案存取

以下列出一些平常會用到的操作資料方法

git : https://raw.githubusercontent.com/todomato/CSharp_IO/master/Program.cs

       static void Main(string[] args)
    {
 
        //取得Bin資料夾
        var bin = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace("file:///", "");
        var path = Path.Combine(Path.GetDirectoryName(bin),"Demo");
        //判斷資料夾位置
        if (!Directory.Exists(path))
        {
            //建立資料夾
            Directory.CreateDirectory(path);
        }
 
        #region 文件相關
        Console.WriteLine(Path.GetDirectoryName(path));     //取得資料夾稱, D:\TestDir
        Console.WriteLine(Path.Combine(new string[] { @"D:\", "BaseDir", "SubDir", "TestFile.txt" })); //產生路徑
        #endregion
 
        //判斷檔案位置
        var filename = "test.txt";
 
        //結合路徑
        var filePath = Path.Combine(path, filename);
 
        //判斷資料夾位置
        if (!File.Exists(filePath))
        {
            //建立檔案
            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
            {
                AddText(fs, "This is some text");
                AddText(fs, "This is some more text,");
                AddText(fs, "\r\nand this is on a new line");
                AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");
            }
        }