【问题标题】:Expects parameter '@CustomerPhoto', which was not supplied需要未提供的参数“@CustomerPhoto”
【发布时间】:2017-04-09 17:05:31
【问题描述】:

我在更新客户表时遇到这个错误:

过程或函数“Update_Customer”需要参数“@CustomerPhoto”,但未提供该参数。

存储过程代码:

ALTER procedure [dbo].[Update_Customer]
    @CustomerID int output,
    @CustomerName nvarchar (50),
    @CustomerPhoto image,
    @CustomerEmail nvarchar(Max),
    @CustomerPhone1 nvarchar(12),
    @CustomerPhone2 nvarchar(12),
    @CustomerAddress nvarchar(Max),
    @CustomerFax nvarchar(12),
    @CustomerStatus bit,
    @CountryID int,
    @CityID int,
    @Notes nvarchar (Max),
    @ModifiedBy nvarchar (30)
AS
BEGIN
    UPDATE CustomersTbl 
    SET CustomerID = @CustomerID,
        CustomerName = @CustomerName,
        CustomerPhoto = @CustomerPhoto,
        CustomerEmail = @CustomerEmail,
        CustomerPhone1 = @CustomerPhone1,
        CustomerPhone2 = @CustomerPhone2,
        CustomerAddress = @CustomerAddress,
        CustomerFax = @CustomerFax,
        CustomerStatus = @CustomerStatus,
        CountryID = @CountryID,
        CityID = @CityID,
        Notes = @Notes,
        ModifiedDate = GETDATE(),
        ModifiedBy = @ModifiedBy
    WHERE
        CustomerID = @CustomerID
END 

数据层类代码:

Friend Function Update_Customer(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
    Dim retval As String

    Dim cmd As New SqlCommand("Update_Customer")

    cmd.Parameters.AddWithValue("@CustomerID", CustomerID)
    cmd.Parameters.AddWithValue("@CustomerName", CustomerName)
    cmd.Parameters.AddWithValue("@CustomerPhoto", SqlDbType.Image).Value = photo
    cmd.Parameters.AddWithValue("@CustomerEmail", CustomerEmail)
    cmd.Parameters.AddWithValue("@CustomerPhone1", CustomerPhone1)
    cmd.Parameters.AddWithValue("@CustomerPhone2", CustomerPhone2)
    cmd.Parameters.AddWithValue("@CustomerAddress", CustomerAddress)
    cmd.Parameters.AddWithValue("@CustomerFax", CustomerFax)
    cmd.Parameters.AddWithValue("@CustomerStatus", CustomerStatus)
    cmd.Parameters.AddWithValue("@CountryID", CountryID)
    cmd.Parameters.AddWithValue("@CityID", CityID)
    cmd.Parameters.AddWithValue("@Notes", Notes)
    cmd.Parameters.AddWithValue("@ModifiedBy", ModifiedBy)

    retval = dm.executeNonQuery(cmd)

    Return retval
End Function

业务层类代码:

Public Function Update_Customer_WithOutPic(ByVal CustomerID As String, ByVal CustomerName As String, ByVal CustomerEmail As String, ByVal CustomerPhone1 As String, ByVal CustomerPhone2 As String, ByVal CustomerAddress As String, ByVal CustomerFax As String, ByVal CustomerStatus As Boolean, ByVal CountryID As Integer, ByVal CityID As Integer, ByVal Notes As String, ByVal ModifiedBy As String) As String
    Dim retval As String
    retval = p.Update_Customer_WithOutPic(CustomerID, CustomerName, CustomerEmail, CustomerPhone1, CustomerPhone2, CustomerAddress, CustomerFax, CustomerStatus, CountryID, CityID, Notes, ModifiedBy)
    Return retval
End Function

更新按钮代码:

Dim retval As String = p.Update_Customer(txtCustomerCode.Text, txtCustomerName.Text, txtCustomerEmail.Text, txtCustomerPhone1.Text, txtCustomerPhone2.Text, txtCustomerAddress.Text, txtCustomerFax.Text, CheckBox2.Checked, ComboCustomerCountry.SelectedValue, ComboCustomerCity.SelectedValue, txtCustomernote.Text, FrmMain.LblUserID.Text)

错误:

过程或函数“Update_Customer”需要参数“@CustomerPhoto”,但未提供该参数。

【问题讨论】:

  • 错误已清除:您没有传递参数的值,@CustomerPhoto
  • 这里是 cmd.Parameters.AddWithValue("@CustomerPhoto", SqlDbType.Image).Value = photo
  • 欢迎来到 StackOverflow!您的问题措辞得当,遵循How to Ask 的规则。不过,您的标题可能需要一点改进。 +1
  • 你不应该把AddWithValueSqlDbType结合起来,你确定你不是故意要这样做cmd.Parameters.Add("@CustomerPhoto", SqlDbType.Image).Value = photo吗?
  • ntexttextimage 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)varchar(max)varbinary(max)See details here

标签: .net sql-server vb.net stored-procedures


【解决方案1】:

在您的Update_Customer() 函数中,您需要为photo 提供一个值作为Byte() 数组。

这是MSDN documentation 中的一个转述示例:

Class Test
  Sub Save(connectionString As String, photoFilePath As String)
    Dim photo As Byte() = GetPhoto(photoFilePath)

    Using connection As SqlConnection = New SqlConnection(connectionString)
      Using command As New SqlCommand(
        "INSERT INTO Employees (Photo) Values (@Photo)", connection)

        command.Parameters.Add("@Photo", SqlDbType.Image, photo.Length).Value = photo
        connection.Open()
        command.ExecuteNonQuery()
      End Using
    End Using
  End Sub



  Public Shared Function GetPhoto(filePath As String) As Byte()
    Dim photo As Byte()

    Using stream As New FileStream(filePath, FileMode.Open, FileAccess.Read)
      Using reader As New BinaryReader(stream)
        photo = reader.ReadBytes(stream.Length)
      End Using
    End Using

    Return photo
  End Function
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多