SqlBulkCopy and Context Connection = T

I have created an assembly which I load into SQL 2005. However, if I set my connection string = context connection = true... I will get an error saying something like this feature could not be used in this context... So I changed my function to insert each row.... Now the issue I have is the transfer takes 4X as long.... Before I made the change I was using the bulkcopy by specifying the actual connection string....but I also had to specify the password in the string...and since I wanted to get way from this specification...I attempted the context route. So...is there any other way of using the bulkcopy feature or something like it using the context connection?

PrivateSharedFunction BulkDataTransfer2(ByVal _tblNameAsString,ByRef _dtAs DataTable,ByRef emailLogAsString)AsBoolean

Dim successAsBoolean =False

emailLog = emailLog & System.DateTime.Now.ToString &" - bulk transfer2 - " & _tblName & vbCrLf

Dim insertStrAsString ="INSERT INTO " & _tblName &"("

Dim valuesAsString =") Values("

Dim drowAs DataRow =Nothing

Dim dColAs DataColumn =Nothing

'add the column names

ForEach dColIn _dt.Columns

insertStr = insertStr & dCol.ColumnName.ToString &", "

values = values &"@" & dCol.ColumnName.ToString &", "

Next

'remove the last comma & form the final string

insertStr = insertStr.Substring(0, insertStr.Length - 2)

values = values.Substring(0, values.Length - 2)

insertStr = insertStr & values &")"

Dim connStrAsString ="context connection = true"

Dim connAsNew SqlConnection(connStr)

Dim cmdAs SqlCommand =Nothing

Using conn

Try

conn.Open()

ForEach drowIn _dt.Rows

cmd =New SqlCommand(insertStr, conn)

ForEach dColIn _dt.Columns

cmd.Parameters.AddWithValue("@" & dCol.ColumnName.ToString, drow.Item(dCol.ColumnName.ToString))

Next

SqlContext.Pipe.ExecuteAndSend(cmd)

Next

success =True

Catch exAs Exception

emailLog = emailLog & System.DateTime.Now.ToString &" " & ex.ToString & vbCrLf

success =False

Finally

Try

conn.Close()

conn.Dispose()

Catch exAs Exception

success =False

EndTry

EndTry

EndUsing

Return success

EndFunction

[7221 byte] By [klegesdal2] at [2007-12-25]
# 1

Not sure if it will be an appropriate solution for you, but that's how I do it when dealing with mass-insert and when I know exact structure of the data:

1. I create a CLR table-valued function that receives something (in your case it might be an XML-serialized DataTable object, but I would prefer to get rid of DataTables at all).
2. CLR-TVF deserealizes the Datatable and returns its rows having FillRow method extracting DataRow's column data into columns of TVF.
3. Use this CLR TVF to insert data with "INSERT ... FROM CLR_TVF()" statement.

SergeiAlmazov at 2007-8-31 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 2
?

Why not use the connection string (i.e.,

non-context connection), but use integrated authentication? Then you won't

have to hardcode any passwords...

I'm not sure why MS disabled SqlBulkCopy inside CLR

routines, but I agree that it would be useful in some cases. If this is

something you'd like to see, why not file an issue at connect.microsoft.com, and

post back the URL to the issue here so that I can vote for

it!


--


Adam Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

< href="mailto:klegesdal2@discussions.microsoft.com">klegesdal2@discussions.microsoft.com>

wrote in message href="news_3Af4f7c7ff-3ab8-440e-a3f9-9dff02b29651_40discussions.microsoft.com">news:f4f7c7ff-3ab8-440e-a3f9-9dff02b29651@discussions.microsoft.com...

I have created an assembly which I load into SQL 2005. However, if I set my

connection string = context connection = true... I will get an error saying

something like this feature could not be used in this context... So I changed

my function to insert each row.... Now the issue I have is the transfer takes

4X as long.... Before I made the change I was using the bulkcopy by specifying

the actual connection string....but I also had to specify the password in the

string...and since I wanted to get way from this specification...I attempted

the context route. So...is there any other way of using the bulkcopy feature

or something like it using the context connection?

size=2>

Private size=2>Shared size=2>Function BulkDataTransfer2( color=#0000ff size=2>ByVal _tblName color=#0000ff size=2>As size=2>String, size=2>ByRef _dt size=2>As DataTable, size=2>ByRef emailLog size=2>As size=2>String) size=2>As size=2>Boolean

Dim success

As color=#0000ff size=2>Boolean = size=2>False

emailLog = emailLog & System.DateTime.Now.ToString & color=#800000 size=2>" - bulk transfer2 - " & _tblName

& vbCrLf

Dim insertStr

As color=#0000ff size=2>String = size=2>"INSERT INTO " & _tblName & color=#800000 size=2>"("

Dim values color=#0000ff size=2>As size=2>String = ")

Values("

Dim drow color=#0000ff size=2>As DataRow = color=#0000ff size=2>Nothing

Dim dCol color=#0000ff size=2>As DataColumn = color=#0000ff size=2>Nothing

'add the column names

size=2>

For color=#0000ff size=2>Each dCol size=2>In _dt.Columns

insertStr = insertStr & dCol.ColumnName.ToString & color=#800000 size=2>", "

values = values & "@" size=2> & dCol.ColumnName.ToString & size=2>", "

Next

'remove the last comma & form the

final string

insertStr = insertStr.Substring(0, insertStr.Length - 2)

values = values.Substring(0, values.Length - 2)

insertStr = insertStr & values & size=2>")"

Dim connStr

As color=#0000ff size=2>String = size=2>"context connection = true"

Dim conn color=#0000ff size=2>As size=2>New SqlConnection(connStr)

Dim cmd color=#0000ff size=2>As SqlCommand = color=#0000ff size=2>Nothing

Using conn

Try

conn.Open()

For color=#0000ff size=2>Each drow size=2>In _dt.Rows

cmd = New

SqlCommand(insertStr, conn)

For color=#0000ff size=2>Each dCol size=2>In _dt.Columns

cmd.Parameters.AddWithValue( size=2>"@" & dCol.ColumnName.ToString,

drow.Item(dCol.ColumnName.ToString))

Next

SqlContext.Pipe.ExecuteAndSend(cmd)

Next

success = True

Catch ex color=#0000ff size=2>As Exception

emailLog = emailLog & System.DateTime.Now.ToString & color=#800000 size=2>" " & ex.ToString &

vbCrLf

success = False

Finally

Try

conn.Close()

conn.Dispose()

Catch ex color=#0000ff size=2>As Exception

success = False

End color=#0000ff size=2>Try

End color=#0000ff size=2>Try

End color=#0000ff size=2>Using

Return success

End color=#0000ff size=2>Function

MVPUser at 2007-8-31 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...
# 3
integrated security ....of course.... thanks I appreciate the help.
klegesdal2 at 2007-8-31 > top of Msdn Tech,SQL Server,.NET Framework inside SQL Server...

SQL Server

Site Classified