2011-03-23

[MSSQL]兩個insert的語法

1.將view或是tableB的資料匯到table A


insert into tableA (field1,field2,field3)
select F1,F2,F3 from
tableB
where F4=0

這個方式之前就有了,在這只是記錄一下.

 

2.insert大量資料 (new feature of SQL 2008)


insert into tableA (field1,field2,field3) 
values
('xxxa','xxxxb','xxxxc'),
('xxxa','xxxxb','xxxxc'),
('xxxa','xxxxb','xxxxc'),
('xxxa','xxxxb','xxxxc')


這個是SQL 2008的新功能.


不過用這樣的語法,一次最多只能新增1000筆.


如果要一次新增更多,可以使用BULK INSERT.


 


參考資料:


SQL SERVER – 2008 – Insert Multiple Records Using One Insert Statement – Use of Row Constructor
http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/


BULK INSERT to table with specific columns
http://beyondrelational.com/blogs/madhivanan/archive/2010/03/17/bulk-insert-to-table-with-specific-columns.aspx

2011-03-22

[tip]IIS 7.5 備份及還原

IIS 6的主控台有介面可以備份跟還原IIS的設定,

但是在IIS7之後那個UI的介面就已經從IIS主控台中被拿掉了.

新的介面則變成匯出設定檔跟啟用共享設定檔(不過共享設定檔是比較好用的方式,下一篇再來說匯出跟共用的部分).

所以如果要執行IIS 7.5的備份跟還原的話可以透過下面的指令的達成.

 

在主機上的%windir%\system32\inetsrv\資料夾中有個appcmd的指令.

可以透過這個指令來操作IIS的備份跟還原

 

顯示目前所有的備份

appcmd list backup

新增一個備份

appcmd add backup 備份檔名稱

備份還原

appcmd restore backup 備份檔名稱

刪除備份檔

appcmd delete backup 備份檔名稱

顯示備份指令的參數說名

appcmd backup /?

 

IIS在每次更動後會自動備份,在預設的情況下,備份的紀錄會保留最後10次,如果要增加保留的數量,

可以透過下列指令修改

appcmd.exe set config -section:system.applicationHost/configHistory /maxHistories:"50" /commit:apphost

 


自動備份的資料夾預設是在 %SYSTEMDRIVE%\inetpub\history 下.


而使用指令備份的資料夾則是存放在 %WinDir%\System32\inetsrv\backup


使用list的指令可以看到這兩個位置下的所有備份檔.






!!下面是針對要在不同機器上備份還原,不過實作上一直有問題,所以可以參考看看但不建議這樣做…


 


從ServerA備份的檔案直接複製到ServerB的%SYSTEMDRIVE%\inetpub\history下,


雖然list的時候可以看到這個備份檔,不過如果要restore的話,就會出現下面的錯誤訊息.



ERROR ( hresult:80070005, message:Command execution failed.Access is denied. )


所以要將備份檔放在%WinDir%\System32\inetsrv\backup下才行.


ps.使用appcmd的指令請用系統管理者的身分開啟command list的視窗再執行.


 


從Server A備份後要在Server B還原的時候,如果網站或是application pool有設定使用domain account存取執行的話,還原後應該會有問題,而在修改application pool或是網站存取帳號的時候應該會下面這錯誤.


IIS7_5


這個主要是因為備份的applicationHost.config中會儲存設定的帳號跟密碼,而密碼會透過設定檔中的key加密.


所以這時候這時需要將Server A上設定檔的key匯出,然後在Server B上還原,指令如下:



匯出(在Server A上執行)


aspnet_regiis -px "iisConfigurationKey" "C:\iisConfigurationKey.xml" -pri
aspnet_regiis -px "iisWasKey" "C:\iisWasKey.xml" –pri



匯入(在Server B上執行)


aspnet_regiis -pi "iisConfigurationKey" "C:\iisConfigurationKey.xml"
aspnet_regiis -pi "iisWasKey" "C:\iisWasKey.xml"


ps.不過這部分我在VM上執行一直有問題…所以後來是改用設定檔共享的方式…


 


參考資料:


How to backup/restore IIS7 configuration
http://blogs.iis.net/bills/archive/2008/03/24/how-to-backup-restore-iis7-configuration.aspx


Using IIS 7.0 Configuration History
http://learn.iis.net/page.aspx/129/using-iis-70-configuration-history/


Configuration History <configHistory>
http://www.iis.net/ConfigReference/system.applicationHost/configHistory


Caution while xcopying IIS 7.0 config files
http://blogs.msdn.com/b/webtopics/archive/2009/03/15/caution-while-xcopying-iis-7-0-config-files.aspx

2011-03-21

[Asp.Net]在沒有SMTP的情況下確認發信的程式

.Net 2.0之後提供了一個specifiedPickupDirectory的功能,
讓你在沒有SMTP server的情況下使用資料夾來儲存發信的內容.

使用方式就是在web.config中加入以下的設定

<system.net>
 <mailSettings>
  <smtp deliveryMethod="SpecifiedPickupDirectory">
   <specifiedPickupDirectory pickupDirectoryLocation="C:\MailPickup\" />
  smtp>
 mailSettings>
system.net>

設定完後,發送的信件就會存在指定的資料夾中.

update [2012/02/23] :

如果你是使用在.net 4.0的話,有可能設定完後出現下面這錯誤

The SMTP host was not specified.

這是個已知的問題 :
http://connect.microsoft.com/VisualStudio/feedback/details/539160/smtpclient-reports-invalidoperationexception-when-disposed-immediatelly-after-sending-mail-and-pickup-directory-is-used

而臨時的解決的方式是在Smtp的tag下加個


<network host="localhost" />

完整範例 :
<system.net>
 <mailSettings>
   <smtp deliveryMethod="SpecifiedPickupDirectory">
     <network host="localhost" />
     <specifiedPickupDirectory pickupDirectoryLocation="C:\MailPickup\" />
     smtp>
 mailSettings>
system.net>



在Hyper-V上跑ubuntu 22.04 Desktop, 裝完後一登入就凍住

如果你也遇到這情況, 你可以執行以下步驟 按 Ctrl+Alt+F3 進入 virtual console. (按Ctrl+Alt+F1 可以再回到GUI) 登入後執行下面指令 (更新kernel) * sudo add-apt-repository ppa:capp...