在不同的行vb.net中记录更新 [英] record update in different row vb.net

查看:77
本文介绍了在不同的行vb.net中记录更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程,在sql server中更新时可以正常工作。这里的问题是执行到vb.net时。更新不同行的记录。从显示的图像中,例如,我已经在datagridview中选择了studentID 383,学生382将被更新。如果我选择了学生380,学生379将会更新,等等。



图片



I have a stored procedure that works perfectly when updating in sql server. The problem here is that when execute to vb.net. Records of different row is updated. From the image shown,For instance i have selected the studentID 383 in datagridview, student 382 will be updated. and if i selected student 380 ,student 379 will be updated and so on.

Image

ALTER PROCEDURE [dbo].[uspUpdate] 
-- Add the parameters for the stored procedure here 
  @SurName     NVARCHAR(20), 
  @FirstName   NVARCHAR(20), 
  @middleName  NVARCHAR(20), 
  @StudAddress NVARCHAR(20), 
  @Birthday    DATE, 
  @Gender      NVARCHAR(20), 
  @Nationality NVARCHAR(20), 
  @BirthPlace  NVARCHAR(20), 
  @TelNum      NVARCHAR(20), 
  @SWG         NVARCHAR(20), 
  @DWG         DATE , 
  @SLA         NVARCHAR(20), 
  @Note        NVARCHAR(20), 
  @StudPic IMAGE , 
  @FFirstName  NVARCHAR(20), 
  @FLastName   NVARCHAR(20), 
  @FMI         NVARCHAR(20), 
  @FOccupation NVARCHAR(20), 
  @FTelNum     NVARCHAR(20), 
  @MFirstName  NVARCHAR(20), 
  @MLastName   NVARCHAR(20), 
  @MMI         NVARCHAR(20), 
  @MOccupation NVARCHAR(20), 
  @MTelNum     NVARCHAR(20), 
  @CFirstName  NVARCHAR(20), 
  @CLastName   NVARCHAR(20), 
  @CMI         NVARCHAR(20), 
  @CAddress    NVARCHAR(20), 
  @CTelNum     NVARCHAR(20), 
  @CMobile     NVARCHAR(20), 
  @studID      INT 
AS 
  BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET nocount ON; 
    BEGIN try 
      BEGIN TRAN 
      UPDATE parentinformation 
      SET    father_firstname = @FFirstName, 
             father_lastname = @FLastName, 
             father_mi = @FMI, 
             father_occupation = @FOccupation, 
             father_telnum = @FTelNum , 
             mother_firstname = @MFirstName, 
             mother_lastname = @MLastName, 
             mother_mi = @MMI, 
             mother_occupation = @MOccupation, 
             mother_telnum = @MTelNum, 
             contact_firstname = @CFirstName , 
             contact_lastname = @CLastName, 
             contact_mi = @CMI, 
             contact_mobile = @CMobile, 
             contact_telnum = @CTelNum 
      FROM   parentinformation PI, 
             studentinformation SI 
      WHERE  pi.parentid = si.parentid 
      AND    pi.parentid = @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 
      FROM   parentinformation PI, 
             studentinformation SI 
      WHERE  pi.parentid = si.parentid 
      AND    pi.parentid = @studID 
      COMMIT TRAN 
    END try







vb.net执行时代码。






vb.net code when executing.

 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("@SurName", SqlDbType.VarChar, 100).Value = txtStudLN.Text
            cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = txtStudFN.Text
            cmd.Parameters.Add("@middleName", SqlDbType.VarChar, 100).Value = txtStudMN.Text
            cmd.Parameters.Add("@StudAddress", SqlDbType.VarChar, 100).Value = txtAddress.Text
            cmd.Parameters.Add("@BirthDay", SqlDbType.VarChar, 100).Value = dtpBirthday.Text
            cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 100).Value = Male
            cmd.Parameters.Add("@Nationality", SqlDbType.VarChar, 100).Value = cboNationality.Text
            cmd.Parameters.Add("@BirthPlace", SqlDbType.VarChar, 100).Value = txtPlaceOfBirth.Text
            cmd.Parameters.Add("@TelNum", SqlDbType.VarChar, 100).Value = txtStudentCP.Text
            cmd.Parameters.Add("@SWG", SqlDbType.VarChar, 100).Value = txtSWG.Text
            cmd.Parameters.Add("@DWG", SqlDbType.VarChar, 100).Value = dtpDWG.Text
            cmd.Parameters.Add("@SLA", SqlDbType.VarChar, 100).Value = txtSLA.Text
            cmd.Parameters.Add("@Note", SqlDbType.VarChar, 100).Value = txtNote.Text
            cmd.Parameters.Add("@FFirstName", SqlDbType.VarChar, 100).Value = txtFatherGN.Text
            cmd.Parameters.Add("@FLastName", SqlDbType.VarChar, 100).Value = txtFatherLN.Text
            cmd.Parameters.Add("@FMI", SqlDbType.VarChar, 100).Value = txtFatherMI.Text
            cmd.Parameters.Add("@FOccupation", SqlDbType.VarChar, 100).Value = txtFatherOccupation.Text
            cmd.Parameters.Add("@FTelNum", SqlDbType.VarChar, 100).Value = txtFatherCP.Text
            cmd.Parameters.Add("@MFirstName", SqlDbType.VarChar, 100).Value = txtMotherGN.Text
            cmd.Parameters.Add("@MLastName", SqlDbType.VarChar, 100).Value = txtMotherLN.Text
            cmd.Parameters.Add("@MMI", SqlDbType.VarChar, 100).Value = txtMotherMI.Text
            cmd.Parameters.Add("@MOccupation", SqlDbType.VarChar, 100).Value = txtMotherOccupation.Text
            cmd.Parameters.Add("@MTelNum", SqlDbType.VarChar, 100).Value = txtMotherCP.Text
            cmd.Parameters.Add("@CFirstName", SqlDbType.VarChar, 100).Value = txtContactGN.Text
            cmd.Parameters.Add("@CLastName", SqlDbType.VarChar, 100).Value = txtContactLN.Text
            cmd.Parameters.Add("@CMI", SqlDbType.VarChar, 100).Value = txtContactMI.Text
            cmd.Parameters.Add("@CAddress", SqlDbType.VarChar, 100).Value = txtContactAddress.Text
            cmd.Parameters.Add("@CTelNum", SqlDbType.VarChar, 100).Value = txtContactTelNum.Text
            cmd.Parameters.Add("@CMobile", SqlDbType.VarChar, 100).Value = txtContactCP.Text
            cmd.Parameters.Add(New SqlClient.SqlParameter("@StudPic", SqlDbType.Image)).Value = IO.File.ReadAllBytes(a.FileName)
            cmd.ExecuteNonQuery()
            MsgBox("Save Updated Successfully")
End using

推荐答案

这个问题与此无关SQL代码。



它与您放入参数的值有关,特别是记录的ID。



传递的值是什么?这就是调试器非常方便的地方。我建议你学习如何使用它,如何使用断点和检查变量内容。



来自网格的选定值显然不是你认为的值它是。
This problem has nothing to do with the SQL code.

It has to do with the values you're placing into the parameters, specifically, the ID of the record.

What are the values being passed in? That's where the debugger is going to come in very handy. I suggest you learn how to use it, how to user breakpoints and inspect variable contents.

The "selected value" coming from the grid is obviously not the value you think it is.


这篇关于在不同的行vb.net中记录更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆