在vb.net中使用图像更新记录

时间:2016-01-03 06:08:41

标签: sql-server vb.net

此代码在使用图像更新数据时有效。但问题是,当我编辑现有的记录时,我不会更改图像。 picturebox变为空。

 Using cmd As New SqlClient.SqlCommand("dbo.uspUpdate", cn)
        cmd.Parameters.AddWithValue("@studID", frmView.dgv1.SelectedCells(0).Value)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(New SqlParameter("@StudPic", SqlDbType.Image))
        If (Not String.IsNullOrEmpty(Me.Name) AndAlso System.IO.File.Exists(a.FileName)) Then
            cmd.Parameters("@StudPic").Value = System.IO.File.ReadAllBytes(a.FileName)
            cmd.Parameters.Add("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
   Else

    cmd.Parameters("@StudPic").Value = DBNull.Value 
            cmd.Parameters.Add("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
Endif

Picture box

中选择图片时的代码
  Private Sub PictureBox1_DoubleClick(sender As Object, e As EventArgs) Handles PictureBox1.DoubleClick
        Dim picl As String
        a.Filter = Nothing
        picl = a.FileName
        a.ShowDialog()
        PictureBox1.Image = Image.FromFile(a.FileName)
    End Sub

BEGIN TRAN

更新代码

UPDATE p 
            SET p.father_firstname = @FFirstName, 
                p.father_lastname = @FLastName, 
                p.Father_MI = @FMI,
                p.Father_Occupation = @FOccupation, 
                p.Father_TelNUm = @FTelNum ,
                p.Mother_FirstName = @MFirstName, 
                p.Mother_LastName = @MLastName, 
                p.Mother_MI = @MMI,
                p.Mother_Occupation = @MOccupation, 
                p.Mother_TelNum = @MTelNum,
                p.Contact_FirstName = @CFirstName ,
                p.Contact_LastName = @CLastName,
                p.Contact_MI = @CMI, 
                p.Contact_Mobile = @CMobile, 
                p.Contact_TelNum = @CTelNum 
                FROM parentinformation p 
                INNER JOIN studentinformation s 
                ON p.parentid = s.parentid 
                WHERE s.StudentID = @studID;
                UPDATE StudentInformation 
                SET Surname = @SurName,
                FirstName = @FirstName,
                MiddleName = @middleName,
                StudAddress =@StudAddress,
                BirthDay = @Birthday, 
                Gender = @Gender, 
                Nationality = @Nationality,
                BirthPlace = @BirthPlace, 
                TelNum = @TelNum, 
                SchoolWhereGraduated = @SWG,
                DatesWhenGraduated = @DWG, 
                SchoolLastAttended = @SLA,
                Note = @Note,
                StudImage = @StudPic        
                where StudentID = @studID

        COMMIT TRAN

1 个答案:

答案 0 :(得分:1)

在SQL中,您可以更改以下行

 StudImage = @StudPic

 StudImage = ISNULL(@StudPic, StudImage)

这将检查传递的参数@StudPic是否为NULL,如果是,则只更新图像的当前值。

如果不从VB.Net传递,则需要将@StudPic参数值默认为NULL。这个link告诉您如何设置存储过程参数的默认值。