2013年10月21日 星期一

SQLCLR FormsAuthentication.HashPasswordForStoringInConfigFile

人客給了一個EXCEL,要批次先建好系統使用者
但因預設密碼為使用者帳戶,每個人都不同,我又想直接透過SQLServer openrowset 方式讀取excel檔然後直接轉入。

首選當然是建一個CLR Funciton 直接在tsql 使用,但CLR不支援System.Web,所以FormsAuthentication.HashPasswordForStoringInConfigFile 就沒法使用了。

找到可行的解決方法,改寫成CLR Funciton ,記錄一下 ,粉方便~~~

What will give me the same result as FormsAuthentication hashing for a non web application?


public static string SHA1(this string stringToHash)
{
    // SHA1 is 160bit
    SHA1 sha = new SHA1Managed();
    byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(stringToHash));

    StringBuilder stringBuilder = new StringBuilder();
    foreach (byte b in hash)
    {
     stringBuilder.AppendFormat("{0:x2}", b);
    }
    return stringBuilder.ToString().ToUpper(); // HashPasswordForStoringInConfigFile uses uppercase hex
}
 
 

2013年10月20日 星期日

OPENROWSET導致SQL Server服務關閉

Windows 8 + SQL 2012 (11.0.3128)

症頭是使用OPENROWSET導致SQL Server服務關閉。
 select * 
  from OPENROWSET('Microsoft.ACE.OLEDB.12.0',
  'Excel 12.0;HDR=YES;IMEX=1;Database=c:\temp\ImportUsers.xlsx',
  'select * from [Users$]')  


查看事件檢視器,就這麼簡短的一行字
SQL Server (MSSQLSERVER) 服務意外地終止。已經發生 1 次。

另外還有項看不懂的Application Error
失敗的應用程式名稱: sqlservr.exe,版本: 2011.110.3128.0,時間戳記: 0x50deadad
失敗的模組名稱: ntdll.dll,版本: 6.2.9200.16579,時間戳記: 0x51637f77
例外狀況代碼: 0xc0000374
錯誤位移: 0x00000000000ebd59
失敗的處理程序識別碼: 0x61c
失敗的應用程式開始時間: 0x01cecd9d9186c411
失敗的應用程式路徑: C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\sqlservr.exe
失敗的模組路徑: C:\Windows\SYSTEM32\ntdll.dll
報告識別碼: af519ed8-3991-11e3-be9a-0c6076872559
失敗的套件完整名稱:
失敗的套件相關應用程式識別碼:




 同一個EXCEL檔轉成ImportUsers.xls使用SQL 2005讀取,卻沒有問題
 select * 
  from OPENROWSET('Microsoft.Jet.OLEDB.4.0',
  'Excel 8.0;HDR=YES;IMEX=1;Database=c:\temp\ImportOrgnUsers.xls',
  'select * from [Users$]')   


 找到這篇文章,解藥如下

EXEC master.dbo.sp_MSset_oledb_prop 
N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1  

EXEC master.dbo.sp_MSset_oledb_prop

 N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters',1

試了一下,執行 AllowInProcess即可解決。

順便查了一下何謂 AllowInProcess  Anish Shenoy說到

AllowInProcess=0 (default)
Running the process outside the SQL Server Process:

Allowing the provider to run outside the SQL Server Process helps to protect the SQL Server process from errors in the provider, as when the provider runs outside the SQL Server process Updates or inserts involving column data types like (varchar(max), nvarchar(max), varbinary(max), text, ntext, or image) are not allowed.


AllowInProcess=1
Running the process inside the SQL Server Process:
By setting this option in the SQL Server providers, we will be able to handle the data types like (varchar(max), nvarchar(max), varbinary(max), text, ntext, or image) including long columns, image and text data.

In case of excel data we can see that when we import an excel data into the database tables without having predefined datatypes, by default the datatypes of some of the columns may be assigned to nvarchar(max) or ntext etc, so if we do net set the Allow Inprocess it will throw error.
 
這讓我順便抱怨一下,在SQL2012使用 撰寫SQL 指令 Import /Export資料時,在資料表對映無法解析到正確的資料類型 ,datatype 202 是什麼東西啊。



剛好也了解到原來這算是是一個BUG嗎?

nvarcahr 和varchar在SP1取消支援,改成了[DT_WSTR]和[DT_STR],把SSIS的定義套用在SSMS,真是很奇怪。

2013年10月18日 星期五

t400 安裝windows8 當當當

前三個月將t400裝上了windows8,除了界面灰常不習慣之外,再來就是三不五時熱當的問題不勝其擾。
前者給安裝了classical shell後,又回到了熟悉的程式集界面了....

後者想了很久,也熱當了幾十次後,後來想說就把獨立顯卡給關了看看,至今三個星期,再也沒有當過。

這這這叫我下次怎麼還買得下手小黑啊......

聯想的小黑啊,你的品質何時可以回升啊,我還是愛死你的鍵盤啊~~~

2013/11/17
得到了一個結論,當機時間都是在較長時間不使用電腦時發生的,所以做了一個測試,把顯卡調回獨顯,在OS電源管理中 ,將所有設定都設為永不,這才真正解決了當機的問題。
原本只有將讓電腦睡眠設為永不
後來連同讓顯示器變案及關閉顯示器都設為永不,就再也不當了.

2013年10月5日 星期六

tsql binary_checksum

通常在SQL Server 安裝時,我都選擇 Chinese_Taiwan_Stroke_CI_AS
至於相容層級怎麼選? 有80, 90,100 ,

但說實在的,有什麼差別,我倒是沒仔細想過....


參考這篇MSDN上的說明,和「補充字元」(Supplementary Character)排序相關吧~(阿災)


 既然資料庫內資料不分大小寫,那當真的有需求要比較大小寫時,就可以用binary_checksum

 select  case when 'mmdd' = 'MMDD' then 1 else 0 end as '不分大小寫'
,case when 'A' = 'A' then 1 else 0 end '不分全半型'

,case when  binary_checksum('mmdd') = binary_checksum('MMDD') then 1 else 0 end '分大小寫'

publish error allowDefinition='MachineToApplication'

一個老舊的aspx web form專案,調了一些功能建置成功,但進行部署時顯示以下錯誤。 在應用程式層級之外使用註冊為 allowDefinition='MachineToApplication' 的區段發生錯誤。錯誤的原因可能是虛擬目錄尚未在 IIS 中設定為...