存储过程插入截断值

问题描述:

我有一个使用多个输入参数并将它们插入Sql数据库的存储过程。有一个参数@CustomerId在插入数据库时​​被截断(但不总是)。存储过程插入截断值

C#:

cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@CustomerId", Convert.ToInt32(customerId)); 
cmd.Connection = sqlConn; 
sqlConn.Open(); 
cmd.ExecuteNonQuery(); 
sqlConn.Close(); 

的Sql SP: @CustomerID诠释

INSERT INTO dbo.tblOffers 
(CustomerId) 
VALUES 
    (@CustomerId) 

SQL表数据类型:

CustomerId int not null 

例如:564276117截断为4276117(但它并非始终发生,并且有大于560000000的值被正确插入) 我在做什么错误?由于

+1

`Convert.ToInt32(customerId)`...'customerId`不是`int`吗?如果在代码的其余部分将它看作是一个“字符串”,那么它在它到达之前可能会被截断? – David 2011-12-15 14:12:56

+0

您应该必须将CustomerId的字段数据类型更改为bigint。 – adatapost 2011-12-15 14:16:28

+0

@AVD - `564276117`在int的范围内。 – 2011-12-15 14:19:10

尝试Parameters.Add()方法,而不是AddWithValue

cmd.Parameters.Add("@CustomerId",SqlDbType.Int).Value=CustomerID; 

请你尝试如下:使用ToInt64

cmd.CommandType = CommandType.StoredProcedure; 
cmd.Parameters.AddWithValue("@CustomerId", Convert.ToInt64(customerId)); 
cmd.Connection = sqlConn; 
sqlConn.Open(); 
cmd.ExecuteNonQuery(); 
sqlConn.Close();