字符串或二进制数据将在Varbinary(MAX)中截断

时间:2016-05-24 11:50:08

标签: c# sql sql-server

我在sql server 2008中创建了一个包含数据类型varbinary(MAX)的列的表。使用C#代码我尝试插入一个pdf文件(18000字节),但它给出了错误" 字符串或二进制数据将被截断"

我经历了很多线程,答案是因为列大小小于数据大小。但Varbinary(MAX)的最大大小为2GB。我需要帮助

CREATE TABLE [dbo].[Purchase_Order] (
[PO_Number]           VARCHAR (25)    NOT NULL,
[Organization_Name]   VARCHAR (10)    NOT NULL,
[PO_Date]             DATE            NOT NULL,
[Value_Amount]        NUMERIC(8, 2)  NOT NULL,
[Currency_Type]       NVARCHAR (10)   NOT NULL,
[Customer_Name]       VARCHAR (50)    NOT NULL,
[Contact_Person]      VARCHAR (50)    NOT NULL,
[Customer_Location]   VARCHAR (20)    NOT NULL,
[End_Date]            DATE            NOT NULL,
[Emug_Contact_Person] VARCHAR (50)    NOT NULL,
[Payment_Terms]       NUMERIC (3)     NOT NULL,
[Project_Type]        NCHAR (10)      NOT NULL,
[File_Name]                VARBINARY (MAX) NOT NULL,
CHECK ([PO_Number]<>''),
CHECK ([Organization_Name]<>''),
CHECK ([PO_Date]<>''),
CHECK ([Value_Amount]<>(0)),
CHECK ([Currency_Type]<>''),
CHECK ([Customer_Name]<>''),
CHECK ([Contact_Person]<>''),
CHECK ([Customer_Location]<>''),
CHECK ([End_Date]<>''),
CHECK ([Emug_Contact_Person]<>''),
CHECK ([Payment_Terms]<>''),
CHECK ([Project_Type]<>''),

);

这是我的插入代码

public int Insertcommand(string tablename, string[] columlist, object[] valuelist)
    {
        int x = 0;
        string commandstring = createcommand(tablename, columlist);
        MyCommand = new SqlCommand(commandstring, GetConnection());

        for (int i = 0; i < valuelist.Count(); i++)
        {
            MyCommand.Parameters.AddWithValue("@" + columlist[i].ToLower(), valuelist[i]);
        }

        x = MyCommand.ExecuteNonQuery();

        return x;
    }

variable names variable values

Mycommand string

qry = "INSERT into Purchase_Order (PO_Number, Organization_Name, PO_Date, Value_Amount, Currency_Type, Customer_Name, Contact_Person, Customer_Location, End_Date, Emug_Contact_Person, Payment_Terms, Project_Type, File_Name) VALUES (@po_number, @organization_name, @po_date, @value_amount, @currency_type, @customer_name, @contact_person, @customer_location, @end_date, @emug_contact_person, @payment_terms, @project_type, @file_name)"

1 个答案:

答案 0 :(得分:3)

这里的问题是Project_Type字段。这里是数据类型长度和实际值的比较:

INSERT into dbo.Purchase_Order
( PO_Number -- 25
, Organization_Name -- 10
, PO_Date
, Value_Amount
, Currency_Type -- 10
, Customer_Name -- 50
, Contact_Person  -- 50
, Customer_Location  -- 20
, End_Date
, Emug_Contact_Person -- 50
, Payment_Terms
, Project_Type -- 10 <--- BAD STUFF
, File_Name -- MAX
)
VALUES
( @po_number -- 10
, @organization_name -- 3
, @po_date
, @value_amount
, @currency_type -- 5
, @customer_name -- 2
, @contact_person -- 3
, @customer_location -- 2
, @end_date
, @emug_contact_person -- 3
, @payment_terms
, @project_type -- 15 <--- BAD STUFF
, @file_name
)

您会看到Project_Type只允许10个字符,但实际值(ONSITE-DOMESTIC)长度为15个字符。