2013年4月15日 星期一

64位元機器上使用CAPICOM

專案上使用到CAPICOM做加解密的功能。

但是部署到 Windows2008R2 會發生錯誤,原因為CAPICOM只支援32位元。

部屬方法:

1.安裝http://www.microsoft.com/en-us/download/details.aspx?id=25281 可取得CAPICOM.DLL

image

2.將capicom.dll複製到C:\Windows\SysWOW64下

3.註冊DLL(以系統管理員身分執行)

cd\
cd c:
cd c:\windows\syswow64
regsvr32.exe c:\windows\syswow64\capicom.dll

4.IIS設定

image

  • 若沒修改會得到(Could not load file or assembly 'Interop.CAPICOM' or one of its dependencies.)錯誤訊息

參考網頁:

  • 使用CAPICOM於64位元系統 (CAPICOM on 64 Bit) Use C# With ASP.NET
  • Using CAPICOM on Windows x64(http://unmitigatedrisk.com/?p=246)
  • 2013年4月6日 星期六

    Sqlite 設定(C#–Web專案)

    image
      • 參考DLL
      • 加入SQLiteHelper
      • 設定web.config
    image
    原則上設定都跟winform設定差不多。
    • 執行後會發生system.Data.Sqlite 參考的錯誤訊息,這邊提供的解法為:
    使用本機IIS Web伺服器的方式瀏覽。
    image
    • 執行畫面:(範例為北風資料庫的訂單)

      image
    • 程式碼下載:
    打包

    2013年3月22日 星期五

    Sqlite 設定(C# - Winform專案)

    image

    • config設定

    image

      • 設定連線字串
      • 設定Provider
    • 連線寫法(EX:ExecuteNonQuery)
    • SQLiteConnection con = new SQLiteConnection(SQLiteHelper.SQLiteConnectionString);
      SQLiteCommand cmd = new SQLiteCommand(commandText, con);

                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.CommandType = CommandType.Text;
                cmd.Parameters.Clear();
                foreach (SQLiteParameter para in paras)
                {
                    cmd.Parameters.Add(para);
                }

                try
                {
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }
                finally
                {
                    con.Close();
                }

    • 谷歌大神上面可見一些Helper範例(EX:SQLiteHelper可以自行參考)
    • 以Winform為例

    image

    • 專案在開始偵錯(F5)時候發生錯誤,通常是因為平台目標錯誤,
      如64位元的DLL,目標平台選X86這樣就會發生錯誤,
      這邊可選擇Any CPU或相對應的位元平台。
    • 實作如下:
      • 畫面

    image

    • 程式

    image

    • 這邊的範例使用sqlite所提供北風資料庫(northwindEF.db,打包在DLL中)
    • 執行結果:

    SNAGHTML28518ab

    2013年2月23日 星期六

    2013卦象

     

    2013歲次癸巳,癸巳者,長流水。對應卦象為地風升。

    癸者黑水,陰水,活水,柔水,在天為雨露,在地為水脈。

    巳者蛇也。

    癸巳者,黑水蛇也。

    癸者,種子萌發。

    巳者,萬物已成;巳有停止之意,因此亦指草木的增長達到了極限的狀態。

    癸巳者,種子萌發到成熟。

    巽者風也,木也。

    坤者地也,土也。

    癸巳者,地下有木,萌芽而生,有上升之義,故為地風升。

    --

    地風升者。

    有生長,晉升之意,草木生長需循序漸進,非一天能成,所以必須步步為營,才能更上層樓。

    上位者須有德,才能得吉祥,若盲目冒進,終獲災殃。

    2013年1月10日 星期四

    條碼製作

    條碼產生的方法有許多種,常見的方法有:

    • 字型
    • 第三方服務:透過網址產生條碼圖片
    • 使用Libary:
      • Barcode Rendering Framework(barcoderender.codeplex.com)
        • Code 11,Code 25,Code 39,Code 93,Code 128
        • Code EAN 13,Code EAN 8
        • Code PDF417
        • Code QR

    範例:

    private string GetBarcodeImage(string CODE,int BarHeight)
    {
    try
    {
    BarcodeSymbology b = BarcodeSymbology.CodeEan13;
    Image img = BarcodeDrawFactory.GetSymbology(b).Draw(CODE, BarHeight,1);
    return Convert.ToBase64String(UtilityUI.ImageToBuffer(img, System.Drawing.Imaging.ImageFormat.Jpeg));
    }
    catch (Exception ex)
    {
    return ex.ToString();
    }
    }

    /// <summary>
    /// 將 Image 轉換為 Byte 陣列。
    /// </summary>
    /// <param name="Image">Image 。</param>
    /// <param name="imageFormat">指定影像格式。</param>
    public static byte[] ImageToBuffer(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
    {
    if (Image == null) { return null; }
    byte[] data = null;
    using (MemoryStream oMemoryStream = new MemoryStream())
    {
    //建立副本
    using (Bitmap oBitmap = new Bitmap(Image))
    {
    //儲存圖片到 MemoryStream 物件,並且指定儲存影像之格式
    oBitmap.Save(oMemoryStream, imageFormat);
    //設定資料流位置
    oMemoryStream.Position = 0;
    //設定 buffer 長度
    data = new byte[oMemoryStream.Length];
    //將資料寫入 buffer
    oMemoryStream.Read(data, 0, Convert.ToInt32(oMemoryStream.Length));
    //將所有緩衝區的資料寫入資料流
    oMemoryStream.Flush();
    }
    }
    return data;
    }

    //====

    <Image Name="ImgCode">

      <Source>Database</Source>

      <Value>=System.Convert.FromBase64String(Fields!CODEIMG.Value)</Value>

      <MIMEType>image/png</MIMEType>

      <Style>

        <Border>

          <Style>None</Style>

        </Border>

      </Style>

    </Image>

    如何使用LocalReport - WebForm (3)


    實作部分,報表繫結與報表下載

    /// <summary>
            /// 報表繫結
            /// </summary>
            /// <param name="rpt">報表檢視器</param>
            /// <param name="dsName">資料集名稱</param>
            /// <param name="RptPath">報表路徑</param>
            /// <param name="dt">繫結之資料表</param>
            public static void ReportBind(
                ReportViewer rpt, 
                string dsName, 
                string RptPath, 
                DataTable dt)
            {
                //Report存放路徑,EX:~/bin/Report
                string Path = ConfigurationManager.AppSettings["RptPath"].ToString();
                rpt.LocalReport.DataSources.Clear();
                rpt.ProcessingMode = ProcessingMode.Local;
                rpt.LocalReport.ReportPath = 
                    HttpContext.Current.Server.MapPath(
                    string.Format("{0}\\{1}", Path, RptPath));
                rpt.LocalReport.DataSources.Add(new ReportDataSource(dsName, dt));
                rpt.LocalReport.Refresh();

            }
            /// <summary>
            /// 報表繫結
            /// </summary>
            /// <param name="rpt">報表檢視器</param>
            /// <param name="dsName">資料集名稱</param>
            /// <param name="RptPath">報表路徑</param>
            /// <param name="dt">繫結之資料表</param>
            /// <param name="PType">PDF,Excel,Word,Image</param>
            public static void ReportExport(
                ReportViewer rpt,
                string dsName,
                string RptPath,
                DataTable dt,
                string PType)
            {
                ReportBind(rpt, dsName, RptPath, dt);
                //
                Microsoft.Reporting.WebForms.Warning[] tWarnings;
                string[] tStreamids;
                string tMimeType;
                string tEncoding;
                string tExtension;
                //呼叫ReportViewer.LoadReport的Render function,將資料轉成想要轉換的格式,並產生成Byte資料
                byte[] tBytes =
                    rpt.LocalReport.Render(
                    PType, null, out tMimeType,
                    out tEncoding, out tExtension,
                    out tStreamids, out tWarnings);
                //將Byte內容寫到Client
                string DisplayName =
                    rpt.LocalReport.DisplayName == "" ?
                    "report" : rpt.LocalReport.DisplayName;
                if (HttpContext.Current.Request.Browser.Browser == "IE")
                {
                    DisplayName =
                        HttpContext.Current.Server.UrlPathEncode(
                        DisplayName);
                }
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ContentType = tMimeType;
                HttpContext.Current.Response.
                    AppendHeader("Content-Disposition",
                    String.Format("attachment; filename={0}.{1}",
                    DisplayName, tExtension));
                HttpContext.Current.Response.BinaryWrite(tBytes);
                HttpContext.Current.Response.End();
            }

    建置或發行時需要注意的設定:
    • 報表相關的DLL需一設定複製到本機為True
    • 報表專案的報表檔,需要也需要設定為複製
    image
    Web.config設定
    • 報表繫結程式中,取得報表檔的方法會參考此路徑,因為報表專案在發行後會將報表檔案(RDLC)複製到bin目錄下。
    • 在此我們可以將此發行後的檔案隨意搬移,如將檔案移到Report的資料夾,就可以自行定義。
    image
    顯示報表與下載程式碼如下:

    protected void btnShowReport_Click(object sender, EventArgs e)
    {
    UtilityUI.ReportBind(
    this.ReportViewer1
    ,"TB1"
    ,"rpt110.rdlc"
    , GetReportData()
    );
    }

    protected void btnDownload_Click(object sender, EventArgs e)
    {
    this.ReportViewer1.LocalReport.DisplayName = "訂單明細";
    UtilityUI.ReportExport(
    this.ReportViewer1
    , "TB1"
    , "rpt110.rdlc"
    , GetReportData()
    ,"PDF"
    );
    }


    結果畫面如下:
    image

    如何使用LocalReport - WebForm (2)

    開始建立報表,在Report專案中,加入新項目,選擇 Reporting > 報表

    SNAGHTMLc65a43

    加入TypeDataSet(資料集),選擇 資料 > 資料集

    SNAGHTMLc8be43

    在資料集編輯畫面,可以透過伺服器總管新增資料庫table或是自訂table,以下範例為自訂

    image

      • 在畫面上按右鍵 加入 DataTable,命名為TB

    image

    在TB中按右鍵,加入資料行,以下使用者就可自行定義。

    image

    接著就可以開始進行報表設計

    image

    在報表編輯畫面上面,使用資料表元件,之後會要求使用者選擇資料集。在此選擇剛剛建立的資料集

    • 名稱:自訂資料集名稱
      • 註:這邊對應dsName
    • 資料來源:建立好的dsReport
    • 可用資料集:TB;dsReport中的DataTable

    SNAGHTML11ad78f

    設定完成之後畫面如下,資料列即可選到要顯示的欄位

    image