2018-04-16 11:08

[VBA] Read Write UTF-8 File

Dim reader As Object
Set reader = CreateObject("ADODB.Stream")
reader.CharSet = "UTF-8"
reader.LineSeparator = 10
reader.Open
reader.LoadFromFile "C:\test.txt"

Dim textLine As String
Do Until reader.EOS
    textLine = reader.ReadText(-2)
Loop
reader.Close


Dim writer As Object
Set writer = CreateObject("ADODB.Stream")
writer.CharSet = "UTF-8"
writer.Open
writer.WriteText "xxxxxxx" & Chr(10)

writer.SaveToFile "C:\test.txt", 2
writer.Close
2018-04-13 10:59

擴充 LinqToSql 的 Left Join 關連 Table

Linq 在處理 Left Join 的語法有點煩人,想採用 EntityRef 的方式進行查詢,而建立 Entity 的關連表的方式很多,但 DBML 都是透過 Visual Studio 的工具去維護的,手動去修改他實在是不優啊!利用 partial class 的方式就可以避開這種問題。

在 AssociationAttribute 中的 IsForeignKey = false 就會採用 Left Join 進行關連查詢。

using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace MyConsoleApp.Database
{
    public partial class JobCommandMaterial
    {
        private Link<Material> _Material;

        [Association(Storage = "_Material", ThisKey = "MaterialCode", OtherKey = "MaterialCode")]
        public Material Material { get { return _Material.Value; } }
    }
}

參考:
AssociationAttribute 類別 (System.Data.Linq.Mapping)
EntityRef(TEntity) 結構 (System.Data.Linq)
Link(T) 結構 (System.Data.Linq)
2018-04-11 14:07

LinqToSql 替換查詢的 Table

因為封存的 Table 是基於 Base Table 複製出來的,DBML 也不可能一開始就建立這些封存的 Table,但又想用 LinqToSql 去處理資料存取,所以想到用程式去換掉 SQL 裡的 Table Name。

var query = _dc.CtrlCommandRecord.Where(x => x.CreateDate > DateTime.Today);

DbCommand command = _dc.GetCommand(query);
command.CommandText = command.CommandText.Replace("CtrlCommandRecord", "CtrlCommandRecord_2017");
command.Connection = _dc.Connection;
command.Connection.Open();

DbDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
var list =  _dc.Translate<CtrlCommandRecord>(reader);

list.Dump();
2018-04-11 12:30

用 Visual Studio 自動壓縮發佈的程式

針對只能用隨身碟安裝程式,又不想用 Web Deploy 這麼複雜工具,簡單的用壓縮檔來是處理,利用 Visual Studio 的檔案發佈再進行壓縮封裝,在原本的 pubxml 檔的後面加上 ZipPublishOutput Target 來接續發佈處理,這樣在 Visual Studio 在執行發佈時就可以產生壓縮封裝。

<?xml version="1.0" encoding="utf-8"?>
<!--
"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" MySolution.sln /t:Rebuild /p:VisualStudioVersion=14.0;Configuration=Release;DeployOnBuild=true;PublishProfile=Release.pubxml
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>obj\Publish</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings>
  </PropertyGroup>

  <Target Name="ZipPublishOutput" AfterTargets="GatherAllFilesToPublish;WebFileSystemPublish">
    <Message Text="== 封裝 7-Zip 安裝包 ===============================" Importance="high" />
    <PropertyGroup>
      <Today>$([System.DateTime]::Today.ToString("yyyyMMdd"))</Today>
      <zip>"C:\Program Files\7-Zip\7z.exe"</zip>
      <ReleasePath>$(ProjectDir)obj\Release\Package\PackageTmp</ReleasePath>
      <PackagePath>D:\MySolution_$(Today).exe</PackagePath>
    </PropertyGroup>

    <Exec Command='DEL $(PackagePath)' />
    <Exec Command='$(zip) a -t7z -mx9 -sfx7z.sfx -bd $(PackagePath) * | findstr /v "^$"' WorkingDirectory="$(ReleasePath)" />
  </Target>
</Project>