Examples

Current Articles | Categories | Search

By David Dye @ Saturday, May 10, 2008 8:09 AM :: 137 Views :: 0 Comments
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.IO
Imports System.Text.RegularExpressions
 
Partial Public Class clr_sp_hyperlink
    <Microsoft.SqlServer.Server.SqlProcedure()> _
    Public Shared Sub clr_sp_hyperlink(ByVal sInputFile As SqlString, ByVal tbl As SqlString, ByVal col As SqlString)
 
        Dim sContents As String
        Dim sp As SqlPipe = SqlContext.Pipe()
        Dim qry As New SqlCommand()
        Dim stmReader As New StreamReader(sInputFile.ToString)
 
        Try
            'Create a string reader and pass it the input parameter of the
            'file path. The sContents variable will receive the content of
            'the stream reader
            sContents = stmReader.ReadToEnd()
            stmReader.Close()
 
            Dim regExPattern As String
            regExPattern = "<a\s+href\s*=\s*""?([^"" >]+)""?>(.+)</a>"
            Dim re As New Regex(regExPattern, RegexOptions.IgnoreCase)
            Dim m As Match = re.Match(sContents)
            'sp.Send(m.Groups(1).Value & " " & sContents)
 
            While m.Success
 
                Dim str As String
                str = m.Groups(1).Value
 
                'string variable is trimmed from trailing and ending white space to
                'see if single quotes are present
                str.TrimStart()
                str.TrimEnd()
 
                'If single quotes are not the first and last character than they are
                'added for the insert statement
                If str.Substring(0, 1) <> "'" Then
                    str = "'" & str
                End If
 
                If str.Substring(str.Length - 1, 1) <> "'" Then
                    str = str + "'"
                End If
 
                'Build the query using the str variable
                qry.CommandText = " INSERT " & tbl.ToString & "(" & col.ToString & _
                ") VALUES ( " & str & ")"
 
                'This will send the insert command to the Messages tab which will make it
                'easier to troubleshoot
                sp.Send(" INSERT hyper VALUES ( " & str & _
                ")")
 
                'Execute the command object
                sp.ExecuteAndSend(qry)
 
                'Reset the str variable to nothing
                str = ""
                m = m.NextMatch()
 
            End While
 
        Catch ex As Exception
            sp.Send(ex.Message.ToString.Substring(1, 7000))
        End Try
 
    End Sub
End Class
 

SQL CODE HERE

CREATE TABLE dbo.hyper(

    id int IDENTITY(1,1) NOT NULL,
    link varchar(4000) NULL)
   
USE AdventureWorks
EXEC clr_sp_hyperlink 'c:\links.htm',
                  'hyper',
                  'link'
SELECT *
FROM hyper
Posted @ Saturday, May 10, 2008 8:09 AM by David Dye
Previous Page | Next Page
Comments
Currently, there are no comments. Be the first to post one!
You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Vote For It - Server Side Compiles in SQL CLR by Jonathan Kehayias

Greg Low a SQL Server MVP, has submitted a very compelling Connect item 265266, Add server-side compilation ability to SQL CLR which aims to remove the requirement to develop SQLCLR assemblies external to SQL Server.  Read about this suggestion here, and vote for it by visiting the Connect site to let Microsoft know you think it is important.

Using CLR Impersonation to Access Resources Outside of SQL Server by Jonathan Kehayias

Traditionally if you had a need to access a file or other resource outside of SQL Server, the SQL Server service account was required to have appropriate file system access to the folder or path containing the file.  With CLR integration, this is no longer an absolute requirement.  Identity Impersonation will allow you to implicitly or explicitly change the execution context inside of a SQLCLR Function, Procedure, or Trigger. 

Using an Application Configuration (app.config/web.config) File in SQL Server CLR Integration by Jonathan Kehayias

A common part of programming in .NET is to use an configuration file to store configuration information in an easily modifiable location.  The app.config or web.config file is an invaluable inclusion in most .NET projects and developers may need to maintain this functionality as a part of logic sharing between objects in the database and the application as well.  This article will demonstrate how to configure your SQLCLR project to use Configuration Files in SQL.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.6874868 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.