Examples

Current Articles | Categories | Search

By Jonathan Kehayias @ Thursday, April 17, 2008 8:52 PM :: 3171 Views :: 0 Comments

This example provides a framework for the SqlBulkCopy class and its implementation in SQL CLR.

 

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Security.Principal;
 
public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void clr_usp_MoveBulkData()
    {
        DataTable source = new DataTable();
 
        using (SqlConnection cn = new SqlConnection("context connection=true"))
        {
            string sql = "SELECT {columnspec} FROM Source_TableName;";
            cn.Open();
            SqlCommand cmd = new SqlCommand(sql, cn);
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            da.Fill(source);
            cn.Close();
        }
 
        WindowsIdentity currentIdentity = SqlContext.WindowsIdentity;
        WindowsImpersonationContext impersonatedIdentity = currentIdentity.Impersonate();
 
        try
        {
            using (SqlConnection conn = new SqlConnection("Server=ServerName;Database=DestinationDatabase;Integrated Security=true"))
            {
                conn.Open();
                SqlBulkCopy bc = new SqlBulkCopy(conn);
                bc.DestinationTableName = "DestinationTable";
                bc.BatchSize = source.Rows.Count;
                bc.WriteToServer(source);
                conn.Close();
            }
        }
        finally
        {
            impersonatedIdentity.Undo();
        }
    }
};

 

Posted @ Thursday, April 17, 2008 8:52 PM by Jonathan Kehayias
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.2028052 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.