星期日, 1月 27, 2019

[SQL] SQL Profile 側錄指定電腦

同事想透過 SQL Profile 來側錄 TSQL 語法,開啟權限後,竟然是告知完全沒有錄到資料,確認問題點後,筆記問題點

Q1:HostName 在哪,必須先勾選右下角 [顯示所有資料行] 後,再去上方向右拖曳就可以找到 [HostName] 欄位

Q2:篩選條件輸入,個人習慣是利用模糊搜尋方式來進行篩選,也就是輸入 [%關鍵字%] 當成篩選條件

同事問題點在於找不到 HostName 欄位,所以改用 LoginName,LoginName 是輸入 [關鍵字%] 當成篩選條件,但又誤會 LoginName,原來不是 ClinetName\UserName,而是 ServerName\UserName,模糊搜尋習慣,以前也沒有注意到這一點,確認問題點順道長知識

星期五, 1月 25, 2019

[EF] 導覽屬性

透過 LINQ 和導覽屬性來產生預期結果

Model-NavigationContext
namespace NavigationProperty.Models
{
    using System.Data.Entity;

    public partial class NavigationContext : DbContext
    {
        public NavigationContext()
            : base("name=NavigationContext")
        {
        }

        public virtual DbSet<Customer> Customer { get; set; }
        public virtual DbSet<Order> Order { get; set; }
        public virtual DbSet<OrderDetail> OrderDetail { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customer>()
                .HasMany(e => e.Order)
                .WithRequired(e => e.Customer)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Order>()
                .HasMany(e => e.OrderDetail)
                .WithRequired(e => e.Order)
                .WillCascadeOnDelete(false);
        }
    }
}

星期日, 1月 20, 2019

[EF] Connection

第一次操作 EF,簡易記錄操作過程

利用 nuget 先安裝 Entity Frameworks


建立 ADO.NET 實體資料模型


選擇來自資料庫的 Code First


選擇連線


選擇資料表 Person.Person Table


ADO.NET 實體資料模型建立後,會產生 AdventureWorksContext.cs、Person.cs、App.Config 內會有 EF 相關設定和連線字串


App.Config 檔案內容
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="AdventureWorksContext" connectionString="data source=.\SQL2017;initial catalog=AdventureWorks;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
AdventureWorksContext.cs
namespace EFConnection
{
    using System.Data.Entity;

    // 繼承 DbContext
    public partial class AdventureWorksContext : DbContext
    {
        public AdventureWorksContext()
            // App.config 內的連線字串名稱
            : base("name=AdventureWorksContext")
        {
        }

        // Person 資料集
        public virtual DbSet<Person> Person { get; set; }

        // Fluent API 來定義
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>()
                .Property(e => e.PersonType)
                .IsFixedLength();
        }
    }
}
Person.cs
namespace EFConnection
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Person.Person")]
    public partial class Person
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int BusinessEntityID { get; set; }

        [Required]
        [StringLength(2)]
        public string PersonType { get; set; }

        public bool NameStyle { get; set; }

        [StringLength(8)]
        public string Title { get; set; }

        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [StringLength(50)]
        public string MiddleName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }

        [StringLength(10)]
        public string Suffix { get; set; }

        public int EmailPromotion { get; set; }

        [Column(TypeName = "xml")]
        public string AdditionalContactInfo { get; set; }

        [Column(TypeName = "xml")]
        public string Demographics { get; set; }

        public Guid rowguid { get; set; }

        public DateTime ModifiedDate { get; set; }

        // 自行 override,方便資料輸出而已
        public override string ToString()
        {
            return $"{LastName}-{FirstName}";
        }

    }
}
測試 Code
namespace EFConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            var context = new AdventureWorksContext();

            // 在每一次 DbContext 物件運行的過程中,每一段 TSQL 均會在主控台輸出
            context.Database.Log = Console.Write;

            // 顯示連線字串
            Console.WriteLine("----- 顯示連線字串 -----");
            Console.WriteLine(context.Database.Connection.ConnectionString);

            var Data = context.Person.Where(w => w.LastName
                .Substring(0, 1) == "L")
                .Take(10);

            // 顯示 TSQL 語法
            Console.WriteLine("");
            Console.WriteLine("----- 顯示 TSQL 語法 -----");
            Console.WriteLine(Data.ToString());

            // 顯示 10 筆資料
            Console.WriteLine("");
            Console.WriteLine("----- 顯示資料 -----");
            foreach (var item in Data)
            {
                Console.WriteLine(item);
            }
        }
    }
}
沒有開啟 Log 的簡易輸出結果

星期五, 1月 04, 2019

[C#] DataBinding 上顯示民國年

之前以為 DataBinding 沒有辦法轉成民國年,最近才發現原來 DataBinding 是可以設定 CultureInfo,筆記測試結果並整合該篇筆記 - [C#] DataGridView 內顯示民國年

Add 多載說明
[C#] DataBinding 上顯示民國年-2

C# Code
namespace DataBinding4TWDate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string FormatString = "yyy/MM/dd";

        private void Form1_Load(object sender, EventArgs e)
        {
            CultureInfo ci = CultureSetting();

            bindingSource1.DataSource = GetData();
            bindingNavigator1.BindingSource = bindingSource1;

            // TextBox 
            txtEmpNO.DataBindings.Add("Text", bindingSource1, "EmpNO");
            txtEmpName.DataBindings.Add("Text", bindingSource1, "EmpName");
            txtHireDate.DataBindings.Add("Text", bindingSource1, "HireDate", true, DataSourceUpdateMode.OnValidation, null, FormatString, ci);

            // DataGridView
            dataGridView1.DataSource = bindingSource1;
            string ColHireDate = "ColHireDate";
            dataGridView1.Columns[ColHireDate].DefaultCellStyle.FormatProvider = ci;
            dataGridView1.Columns[ColHireDate].DefaultCellStyle.Format = FormatString;
        }

        private CultureInfo CultureSetting()
        {
            CultureInfo ci = new CultureInfo("zh-tw");
            TaiwanCalendar tc = new TaiwanCalendar();
            ci.DateTimeFormat.Calendar = tc;
            return ci;
        }

        private List<Employ> GetData()
        {
            var Data = new List<Employ>()
            {
                new Employ(){ EmpNO = "1" , EmpName = "張三" , HireDate = new DateTime(1999,10,3)} ,
                new Employ(){ EmpNO = "2" , EmpName = "李四" , HireDate = new DateTime(2015,7,7)} ,
                new Employ(){ EmpNO = "3" , EmpName = "王五" , HireDate = new DateTime(2017,5,3)} ,
            };
            return Data;
        }
    }

    public class Employ
    {
        public string EmpNO { get; set; }
        public string EmpName { get; set; }
        public DateTime HireDate { get; set; }
    }
}
[C#] DataBinding 上顯示民國年-1

測試 DateTimePicker 時發現該篇-文章,該控件看起來是無法轉成民國年就是,反正本來就沒有使用它,直接跳過

星期三, 1月 02, 2019

[VFP] Automation 應用 - 允許編輯範圍

突然被使用者提出需求說,要對某份 Excel 設定保護唯讀,但是某欄位要可以進行編輯,直接在 Excel 上進行設定時,直覺是 Excel 2016 上功能,好像跟以前版本不太一樣

[VFP] Automation 應用 - 允許編輯範圍-1

[VFP] Automation 在 Excel 上的應用 筆記中,有發現以前是透過對欄位進行[鎖定]設定來達到需求,測試過在 Excel 2016 上[鎖定]也還是可以用,現在是透過[允許編輯範圍],整合一下兩種用法囉
-- 原設定方式
loExcel.ActiveSheet.Range("目標範圍").Select
loExcel.SELECTION.Locked = .F.
loExcel.ActiveSheet.Protect("密碼")

-- Excel 2016 設定方式
TargetRange = loExcel.ActiveSheet.Range("目標範圍")
-- 範圍名稱可以隨便取,不要重覆就好,預設是範圍1、範圍2 ... 的流水號
loExcel.ActiveSheet.Protection.AllowEditRanges.Add("範圍名稱",TargetRange) 
loExcel.ActiveSheet.Protect("密碼")