Examples

Current Articles | Categories | Search

By Jonathan Kehayias @ Thursday, April 17, 2008 8:52 PM :: 372 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

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.8281091 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.