站內文章

2014年11月28日 星期五

找不到檔案或組件名稱 System.Data.SqlServerCe

問題:
找不到檔案或組件名稱 'System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=3BE235DF1C8D2AD3' 或其相依性的其中之一。

網路上找到有兩種解法(我是用解法一解決)

解法一
把sqlce.wce5.armv4i.CAB放到裝置上面安裝即可

sqlce.wce5.armv4i.CAB的路徑在
C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i

解法二
將System.Data.SqlServerCe.dll、sqlceca35.dll、sqlcecompact35.dll、 sqlceer35CN.dll、sqlceme35.dll、sqlceoledb35.dll、sqlceqp35.dll、sqlcese35.dll、System.Data.SqlClient.dll這幾個檔案丟到裝置上跟應用程式同一個目錄中

原文 http://www.myexception.cn/system/425538.html

2014年11月27日 星期四

System.Text.Encoding.Default 產生亂碼


最近在處理簽章遇到了這個問題,產生的簽章格式是bytearray,想轉成string
一開始用了 string str = System.Text.Encoding.Default.GetString ( byteArray );
結果發現產生的string是亂碼,後來用下面這個方式解決
ByteArrayToHexString


    private string ByteArrayToHexString(byte[] data)
    {
        StringBuilder sb = new StringBuilder(data.Length * 3);
        foreach (byte b in data)
            sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
        return sb.ToString().ToUpper();
    }

2014年11月26日 星期三

String直接換成Bytes


string 長怎樣,轉過去bytes就怎樣
限制string長度必須為偶數

    protected byte[] StringToBytes(string HexString)
    {
        int byteLength = HexString.Length / 2;
        byte[] bytes = new byte[byteLength];
        string hex;
        int j = 0;
        for (int i = 0; i < bytes.Length; i++)
        {
            hex = new String(new Char[] { HexString[j], HexString[j + 1] });
            bytes[i] = HexToByte(hex);
            j = j + 2;
        }
        return bytes;
    }

    private byte HexToByte(string hex)
    {
        if (hex.Length > 2 || hex.Length <= 0)
            throw new ArgumentException("hex must be 1 or 2 characters in length");
        byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
        return newByte;
    }