ASP.NET,SQL Server 2008 R2,System.OverflowException

问题描述:

我想使用存储过程在SQL Server 2008 R2表中插入一条记录。我正在“试图”为数据库访问编写一个通用类。ASP.NET,SQL Server 2008 R2,System.OverflowException

下面是.VB文件中的代码:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then 
     Dim params(5) As SqlParameter 
     params(0) = New SqlParameter("@Name", PreviousPage.Name.Text) 
     params(1) = New SqlParameter("@Qualification", PreviousPage.Qualification.Text) 
     params(2) = New SqlParameter("@ContactNo", PreviousPage.ContactNo.Text) 
     params(3) = New SqlParameter("@Expertise", PreviousPage.Expertise.Text) 
     params(4) = New SqlParameter("@Comments", PreviousPage.Comments.Text) 
     params(5) = New SqlParameter("@Affiliation", PreviousPage.Affiliation.Text) 
     DAL.DoInsertSP(params) 
    End If 
End Sub 

下面是DAL.DoInsertSP()的代码:

Public Shared Function DoInsertSP(ByRef params As Array) As Byte 
    Dim result As Byte = 0 
    CreateConnection() 
    cmd = New SqlCommand("sp_InsertExpert", conn) 
    cmd.CommandType = CommandType.StoredProcedure 
    cmd.Parameters.AddRange(params) 
    Try 
     result = CByte(cmd.ExecuteNonQuery()) 
    Catch ex As Exception 
     conn.Dispose() 
    End Try 
    Return result 
End Function 

的代码抛出System.OverflowException:算术运算导致溢出。

这里的PROC脚本sp_InsertExpert

USE [dbherpes] 

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [DBO]。[sp_InsertExpert]
@Name nvarchar(30)= null,
@Qualification为nvarchar(40)=零,
@ContactNo为nvarchar(13)=零,
@Expertise为nvarchar(30)=零,
@Comments为nvarchar(100)=零,
@Affiliation为nvarchar(50 )= null AS
BEGIN
SET NOCOUNT ON;
INSERT INTO专家(名称,资质,ContactNo,专业知识,评论,所属)VALUES(@Name,@Qualification,@ContactNo,@Expertise,@Comments,@Affiliation)
END

这里的结构表:

enter image description here

我怎样才能解决这个吗?

UPDATE

我已经设置ID列的标识规格=“是”,标识增量= 1,恒等式种子= 1

+2

你的'ID'不应该是int吗? –

+0

将.NET全部切割出来,并使用相同的数据从SQL Managment Studio运行sp_InsertExpert,您会得到什么? –

你没有在其异常是发生的行号?

通过看你的代码:

的ExecuteNonQuery

返回一个int按http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

我建议的输出不转换成字节,而是比较CMD。的ExecuteNonQuery()的值,如> 0

对于UPDATE,INSERT和DELETE语句,返回值是 数目该命令影响的行。当插入或更新的 表中存在触发器时,返回值包括受插入或更新操作影响的行的编号 以及受触发器或触发器影响的行的编号 。对于所有其他类型的 语句,返回值为-1。如果发生回滚,则返回值 的值也是-1。

+0

Daniel A. White提出的解决方案适用于我。在应用他发布的修复之后,我在Management Studio中打开了表,发现“我认为没有存储在数据库中的行”也在修复后插入的行中。 –