0

I Use FileStream to Insert file into column with Varbinaray(Max). This is my sample code :

byte[] dataBytes = File.ReadAllBytes(fileNameAndPath);
string insertCommand = string.Format(@" INSERT INTO [dbo].[ts000Attachments] ([Data]) values   (@Data)");
command.Parameters.Add("@Data",SqlDbType.VarBinary,-1).Value = dataBytes;

command.ExecuteNonQuery();

above code work with .doc file. How to fix this Problem.

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
S.A
  • 50
  • 13
  • This column is probably not a varbinary(max). Prove that it is. – usr Sep 16 '14 at 12:06
  • This error will occur only if column size is lesser than the data which you try to insert into that column. Make sure column with a size of max. – pyborg Sep 16 '14 at 12:14
  • I Create Table With below statement : CREATE TABLE [ts000Attachments] ( [Id] uniqueidentifier ROWGUIDCOL NOT NULL DEFAULT (NEWID()) PRIMARY KEY, [Data] varbinary(max) FILESTREAM NOT NULL, ) GO but is not work and throw exception – S.A Sep 16 '14 at 12:24
  • Replace `dataBytes` by `new byte[1]` and see if the error goes away. If not, what exact exception including stack do you get? – usr Sep 16 '14 at 12:32
  • Only When I Want to insert .docx file see this exception but when i insert the same file with .doc format it is correctly work. For Example : I have a file with name 'test.doc' and this file contain 'hi. Mr.' and when i use above code it is correctly inserted. but when save as the same file by .docx postfix i see above exception. Message Of Exception is : "String or binary data would be truncated.\r\nThe statement has been terminated" – S.A Sep 16 '14 at 12:39
  • Perform the test that I requested. Do not just ignore comments. – usr Sep 16 '14 at 12:41
  • And My File is not greater than 1MB. – S.A Sep 16 '14 at 12:43
  • When i changed databytes to new byte[1] again see "String or binary data would be truncated..." , why? – S.A Sep 16 '14 at 12:50
  • I asked you to provide certain information. Follow up on everything that has been said. – usr Sep 16 '14 at 13:18
  • Thank you all. My Problem Was Solved. I Save File Name And File Data. I had an error in the file name. my file name in table was nvarchar 50 but selected file length was 51. – S.A Sep 17 '14 at 04:54

1 Answers1

0

According to below links, this issue is all about column size in sql:

StackOverflow:

SQL Server String or binary data would be truncated

SQL Server Error : String or binary data would be truncated

Microsoft:

String or binary data would be truncated" and field specifications

CodeProject:

String or binary data would be truncated. The statement has been terminated.

also make sure that FileStream feature is activated on your table. for me I don't know why my table did not create with FileStream Feature. so, I dropped the table and re-create it.

S.A
  • 50
  • 13