please find the example of such SP below:
CREATE PROCEDURE [ExportBlobDataFromTable]
@columns varchar(500) = 'ID, BlobColumn, TextColumn', -- the columns used for the export, comma separated
@table varchar(500) = 'BlobTable', -- the table name used for the export
@outputFileName varchar(200) = 'C:\temp\extract.csv', -- fully_qualified_path_to_file
@bcpCommand varchar(1000)
AS
BEGIN
SET @bcpCommand = 'bcp "SELECT ' + @columns + ' FROM ' + @table + '" queryout "' ++@outputFileName + '" -T -n'
EXEC master..xp_cmdshell @bcpCommand
END;
This will create the SP and you can call it from another window like this:
USE [test]
GO
DECLARE @columns varchar(500)
DECLARE @table varchar(500)
DECLARE @outputFileName varchar(200)
DECLARE @bcpCommand varchar(1000)
SET @columns = 'ID, BlobColumn, TextColumn';
SET @table = 'BlobTable';
SET @outputFileName = 'C:\temp\extract.csv';
EXECUTE [dbo].[ExportBlobDataFromTable]
@columns
,@table
,@outputFileName
,@bcpCommand
GO
Or from your C# method:
private void CallSpFromCode()
{
var sqlConnectionString = "User ID=\"" + username + "\";pwd=\"" + pw + "\";Initial Catalog=master;Data Source=" + dataSource + ";";
using (SqlConnection con = new SqlConnection(sqlConnectionString )) {
using (SqlCommand cmd = new SqlCommand("ExplortBlobDataFromTable", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@columns", SqlDbType.VarChar).Value = "ID, BlobColumn,TextColumn";
cmd.Parameters.Add("@table", SqlDbType.VarChar).Value = "BlobTable";
con.Open();
cmd.ExecuteNonQuery();
}
}
}
I hope this helps :)