Examples

Current Articles | Categories | Search

By David Dye @ Saturday, May 10, 2008 8:09 AM :: 782 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

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.4212027 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.