Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub NullCountSample(ByVal TableName As SqlString)
' SQL インジェクション攻撃を避けるために適切なチェックを実施してください。
'If tablename.Value.IndexOf(";"c) > 1 Then
' SqlContext.Pipe.Send("パラメータにセミコロンは使用できません。")
' Return
'End If
Dim count As Integer
' コンテキスト接続の利用
Using cnn As New SqlConnection("context connection=true")
cnn.Open()
Dim cmd As New SqlCommand("SELECT * FROM " & TableName.Value, cnn)
Dim rdr As SqlDataReader = cmd.ExecuteReader()
Do While rdr.Read()
For i As Integer = 1 To rdr.FieldCount
If rdr.IsDBNull(i - 1) Then count += 1
Next
Loop
SqlContext.Pipe.Send(String.Format("{0} NULL値が存在します", count))
End Using
End Sub
End Class--------------------------------------------------------------
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub TEST_HOGE()
Dim SQL As SqlCommand
Dim vValue As long
Using connection As New SqlConnection("context connection=true")
connection.Open()
Try
SQL = Connection.CreateCommand
SQL.CommandText = "select VALUE from HOGE"
SQL.CommandType = CommandType.Text
Using Rec As SqlDataReader = SQL.ExecuteReader()
Do Until (Not (Rec.Read()))
vValue = CLng(Rec(0).ToString)
'vValueのいろいろ処理をいれてみる
Loop
End Using
Catch ex As Exception
SqlContext.Pipe.Send(ex.Message)
End Try
connection.Close()
End Using
End Sub
------------------------------------------------------------------------
@it sql2005 CLR
サンプル置いときます。