Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a varbinary(max) column that I need to bulk import. The data I'm putting into this column is a byte[] in C#. How do I format the data in the comma-delimited file used for bulk import? In other words, how do I convert from a byte[] to a string in a way the bcp utility will accept?

share|improve this question
    
Did one of these answers work for you please? –  gbn Aug 27 '11 at 12:46
    
Can you add sample data please? My answer won't work for me.. :) –  gbn May 17 '13 at 6:44

2 Answers 2

up vote 3 down vote accepted

A varbinary value is of the form 0x01020304 for SQL Server to read.

So your CSV would be

col1,col2,varbinary
foo,1,0x01020304
bar,2,0x988776655443

Edit May 2013,

After testing, I can't get this to work with BULK INSERT with or without a format file.

I will post something more when I get it working. I can't delete this answer

share|improve this answer
    
Have you used this successfully? On my end, I got "type mismatch" errors until I dropped the hex prefix. "0102" was OK, "0x0101" was not. Perhaps there is a setting somewhere which specifies which method is required. –  Jon of All Trades May 15 '13 at 23:48
    
@JonofAllTrades: hmm I can't get it to work. Playing with format files but that is not working either –  gbn May 16 '13 at 7:05
    
Which one? If "0x00" is what works for you, then it seems some setting differs between our setups. If "00" is what works for you, and "0x00" does not, then we're both seeing the same thing, and it might make sense to edit your answer. –  Jon of All Trades May 16 '13 at 19:49

You can use the BCP unicode native format to import/export. Use bcp -n for the export and the WITH (DATAFILETYPE='widenative') clause for import. Click link to read more.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.