2007年6月21日木曜日

[VB2005] メソッド名を取得する

トレースログなんかで、自メソッドを呼んだのは誰?なんて事を知りたいときが良くあります。
そんなときに使えそうな処理がわかりました。
クラス名とメソッド名が取得されます。

これから、Reflection関係を勉強してみたいです。

---------------------------
  1. Dim st As StackTrace = New StackTrace(False)  
  2. Dim sf As StackFrame  
  3. Dim mb As System.Reflection.MethodBase  
  4.   
  5. Dim sf0 As StackFrame = st.GetFrame(0)  
  6. Dim mb0 As System.Reflection.MethodBase = sf0.GetMethod()  
  7.   
  8. Dim stackLoop As Int32 = 1  
  9.   
  10. Dim className As String = String.Empty  
  11. Dim methodName As String = String.Empty  
  12. Do  
  13.   
  14.     sf = st.GetFrame(stackLoop)  
  15.     mb = sf.GetMethod()  
  16.   
  17.     If (mb.ReflectedType.Name = mb0.ReflectedType.Name) Then  
  18.         stackLoop += 1  
  19.     Else  
  20.         className = mb.ReflectedType.FullName  
  21.         methodName = mb.Name  
  22.         Exit Do  
  23.     End If  
  24.     'まぁ50も見とけば良いでしょう  
  25.     If (50 < stackLoop) Then  
  26.         Exit Do  
  27.     End If  
  28. Loop  

2007年6月8日金曜日

[SQL Server 2005] SQL Serverでテーブル定義取得

SQL Server 2005で、テーブル定義らしいものを取得するクエリです。
これを使って、テーブル定義書を自動生成させてます。

  1. select  
  2. Col.name as '名称'  
  3. ,(Select top 1 name From systypes Where systypes.xtype = Col.xtype) as '型'  
  4. ,Col.length as '桁数'  
  5. ,case isnull(Col.scale,0)  
  6.  when 0 then ' '  
  7.  else cast( Col.scale as char(10) )  
  8.  end '小数部'  
  9. ,case  Col.isnullable  
  10.  when 0 then '○'  
  11.  else ' '  
  12.  end 'NN'  
  13. ,isnull((select case colid when 0 then '' else '○' end  from sysindexkeys as keys where col.id = keys.id  and col.colid = keys.colid and keys.indid = 1),' 'as 'PK'  
  14. ,isnull((select top 1 ex.value from sys.extended_properties as ex where col.id = ex.major_id and ex.minor_id = col.colid and ex.name = 'MS_Description' ),' 'as 'コメント'  
  15. From syscolumns as col  
  16. inner join sysobjects as obj on col.id = obj.id  
  17. Where obj.name = '<テーブル名>'