0

我在 SQL Server 2005 中为员工详细信息创建了一个插入命令的存储过程,其中一个参数是我已用作varbinary表中数据类型的图像。

但是当我在存储过程中添加该参数时,我收到以下错误-

不允许从数据类型 varchar 到 varbinary 的隐式转换。使用 CONVERT 函数运行此查询。

存储过程:

( 
    @Employee_ID nvarchar(10)='', 
    @Password nvarchar(10)='', 
    @Security_Question nvarchar(50)='', 
    @Answer nvarchar(50)='', 
    @First_Name nvarchar(20)='',
    @Middle_Name nvarchar(20)='', 
    @Last_Name nvarchar(20)='', 
    @Employee_Type nvarchar(15)='', 
    @Department nvarchar(15)='', 
    @Photo varbinary(50)='' 
)

insert into Registration
(
      Employee_ID,
      Password,      
      Security_Question,
      Answer,
      First_Name,
      Middle_Name,
      Last_Name,
      Employee_Type,
      Department,     
      Photo     
)
values
(
      @Employee_ID,
      @Password,     
      @Security_Question,
      @Answer,
      @First_Name,
      @Middle_Name,
      @Last_Name,
      @Employee_Type,
      @Department,      
      @Photo   
     )

表结构:

Column Name        Data Type                 Allow Nulls
Employee_ID    nvarchar(10)             Unchecked
Password       nvarchar(10)              Checked
Security_Question   nvarchar(50)         Checked
Answer             nvarchar(50)              Checked
First_Name     nvarchar(20)              Checked
Middle_Name    nvarchar(20)              Checked
Last_Name      nvarchar(20)              Checked
Employee_Type      nvarchar(15)              Checked
Department     nvarchar(15)              Checked
Photo              varbinary(50)         Checked

vb.net中调用存储过程的代码->

公共函数提交(ByVal obj As UserData,ByVal opt As String)作为布尔值

    Using cnn As New SqlConnection(conn)
        Using cmd As New SqlCommand
            cmd.Connection = cnn
            If opt = "Submit" Then
                cmd.CommandText = "sp_emp_Registration"
            End If

            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add(New SqlParameter("@Employee_ID", obj.EmpID))
            cmd.Parameters.Add(New SqlParameter("@Password", obj.Pwd))
            cmd.Parameters.Add(New SqlParameter("@Security_Question", obj.SecQues))
            cmd.Parameters.Add(New SqlParameter("@Answer", obj.Ans))
            cmd.Parameters.Add(New SqlParameter("@First_Name", obj.Firstname))
            cmd.Parameters.Add(New SqlParameter("@Middle_Name", obj.Middlename))
            cmd.Parameters.Add(New SqlParameter("@Last_Name", obj.Lastname))
            cmd.Parameters.Add(New SqlParameter("@Employee_Type", obj.EmpType))
            cmd.Parameters.Add(New SqlParameter("@Department", obj.dept))                
            cmd.Parameters.Add(New SqlParameter("@Photo", obj.photo))

            cnn.Open()
            Try
                If (cmd.ExecuteNonQuery() > 0) Then
                    cnn.Close()
                    Return True
                Else
                    Return False
                End If
            Catch ex As Exception
                Return False
            End Try
        End Using
    End Using
End Function

我不知道该怎么办..谁能给我一些建议或解决方案?提前致谢。

4

2 回答 2

1

You assign a string to a varbinary as default value. This operation don't perform a implicit cast. To avoid error:

Change line:

@Photo varbinary(50)='' 

by:

@Photo varbinary(50)

If you don't have Photo value for some rows you should alter table column to allow nulls.

于 2012-10-07T09:35:02.893 回答
0

The problem is @Photo varbinary(50)=''. In that statement you are setting the varbinary variable to a string value, i.e. = ''. Don't do that.

于 2012-10-07T09:35:24.040 回答