2014年11月24日 星期一

Coding First Data Annotations (Model屬性的相關標註說明)

假設有一個Model的類別
    public class User {
        public int Id { get; set; }
        public string PersonalId { get; set; }
        public string Name { get; set; }
    }

[Key]:索引鍵
        [Key]
        public int Id { get; set; }

[Key,Column(Oerder=n)]複合索引鍵:
        [Key]
        [Column(Order=1)]
        public string PersonalId { get; set; }
        [Key]
        [Column(Order=2)]
        public string Name {get;set;}

[Required]:必要欄位不可為空

        [Required]
        public int Id { get; set; }

[MaxLength(n),ErrorMessage=”your message“,MinLength(n)]:最大和最小值和自訂錯誤訊息
        [MaxLength(2),ErrorMessage=”姓名欄位請輸入2~5個字“,MinLength(5)]
        public string Name { get; set; }   

[NotMapped]:不須建立資料庫的欄位
        [NotMapped]
        string sex = PersonalId.Substring(1, 1)=="1"?"男":"女"; 

[Table][Column]:自訂資料庫表格和欄位名稱
        [Table("MyUser")]
        public class UserModel{
            [Column("Myid",TypeName="ntext")]
            string PersonalId{get;set;}
       }

[Index]:欄位建立搜尋索引EF6.1以後版本才有此功能,預設的索引名稱是IX_
        [Index("MyIndexName")]
        public string Name {get;set;}
       }



需要參考以下兩個組件:
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 


2014年11月23日 星期日

各版本的Sql connection 字串寫法

LocalDB:
<add name="ConnectionStringName" providerName="System.Data.SqlClient"  
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />

Sql Express:
<add name="ConnectionStringName" providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>
Sql Server:
<add name="ConnectionStringName" providerName="System.Data.SqlClient"
    connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />                        

SQL Azure:
<add name="ConnectionStringName"  providerName="System.Data.SqlClient"
    connectionString="Data Source=tcp:ServerName.database.windows.net,1433;Initial Catalog=DatabaseName;Integrated Security=False;User Id=username@servername;Password=password;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" />

Entity Framework Database:
<add name="ConnectionStringName"  providerName="System.Data.EntityClient"
    connectionString="metadata=res://*/ ContextClass.csdl|res://*/ ContextClass.ssdl|res://*/ ContextClass.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=ServerName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True&quot;" />

Sql Compact:
<add name="ConnectionStringName" providerName="System.Data.SqlServerCe.4.0"
    connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" />

2014年11月20日 星期四

Adjacent selectors的用法,li中的範例


<style  type="text/css">
li{
color: maroon;
}
li+li{
color: silver;
}
li+li+li{
color: blue;
}
</style>


<body>

<ol>
<li>第1個重點</li>
<li>第2個不太重要</li>
<li>第3個太不重要了</li>
</ol>

</body>


結果:



2014年11月19日 星期三

child selector 與 descendant selector的差異性

<style  type="text/css">
div{
font-size:x-large;
background-color:black;
}

//descendant selector 後代
div  span{
background:red;
}

//child selector 孩子
div > span{
background:blue;
}

</style>

<body>
<div><span>123</span><span>456<span>789</span></span></div>
</body>



結果:





2014年11月12日 星期三

[Database] 在Ubuntu安裝sqlite和sqlitebrowser


安裝sqlite

sudo apt-get install sqlite3


試看看是否可以登入sqlite3 (資料庫不存在的話會自動建立)

sqlite3 [DatabaseName]

例:sqlite3 test.db

.help 顯示相關命令
.databases 顯示資料庫
.table 顯示表格
.exit or .quit 離開























安裝sqlite browser

英文文件請參考:https://github.com/sqlitebrowser/sqlitebrowser/blob/master/BUILDING.md


請先安裝libqt4-dev和libsqlite3-dev 和 qp-sdk

sudo apt-get install libqt4-dev

sudo apt-get install libsqlite3-dev

sudo apt-get install qp-sdk


下載:source 檔案 

https://github.com/sqlitebrowser/sqlitebrowser/zipball/master

更多檔案格式請見官網:http://sqlitebrowser.org/

============安裝=================


  1. 打開終端機進到你的解壓縮的目錄
  2. 建立build資料夾 mkdir build
  3. 進去資料夾 cd build
  4. 執行cmake ..
  5. make 
  6. sudo make install 

註.cmake前請先檢查寫入權限問題,請用chmod改變權限


make過程












sudo make install 








再來執行 /usr/local/share/applications/sqlitebrowser.desktop 就可以了
















圖形還是比較人性化,這樣建schema就大大方便了




















出現could not exec gnu/qt4/bin/qmake 的錯誤就是沒有安裝上面libqt4-dev和libsqlite3-dev 






2014年11月9日 星期日

[NO SQL] MongoDB 安裝


原文內容請直接參考:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/


第一步:取得金鑰
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10









第二步:增加更新來源的網址

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list








第三步:sudo apt-get update

第四步:安裝mongoDB


sudo apt-get install -y mongodb-org













MongoDB資料放在/var/lib/mongodb
log檔放在 /var/log/mongodb

啟動設定檔放在/etc/mongod.cnf,使用port:27017為預設連接port

啟動:sudo service mongod start
停止:sudo service mongod stop








2014年11月8日 星期六

apt-get 常用指令


apt-cache search package 搜索程式
apt-cache show package 取得程式的訊息
sudo apt-get install package 安装程式
sudo apt-get install package - - reinstall 重新安装程式
sudo apt-get -f install 修复安装      "-f = --fix-missing"
sudo apt-get remove package 移除程式
sudo apt-get remove package - - purge 删除程式,包括删除配置文件等
sudo apt-get update 更新程式
sudo apt-get upgrade 更新已安装的包
sudo apt-get dist-upgrade 升级系统
sudo apt-get dselect-upgrade 使用 dselect 升级
apt-cache depends package 了解程式依存關係
sudo apt-get build-dep package 安装編譯的程式
apt-get source package 下載程式的開源碼
sudo apt-get clean && sudo apt-get autoclean 清理無用的檔案
sudo apt-get check 檢查程式的依存關係是否有損壞

2014年11月2日 星期日

char欄位插入中文字與英文字的差別


建立一個char(5)的文字欄位

試著新增‘中字長度六個’的文字






顯示警告,仍然新增成功,將多餘的字元截斷












新增‘1234567890a’的11個英文字











出乎意料,英文字也是在第六個字就截斷了











所以char(5)在中文字和英文字的結果都是一樣的

2014年11月1日 星期六

查詢文字欄位的長度




select length('中'),佔3個byte













select bit_length('中'),佔了24個bit











select char_length('中'),得到1個中文字












結論:計算中文字長度要用char_length()函數


熱門文章