我有一个存储过程,在sql server中更新时可以正常工作。这里的问题是执行到vb.net时。更新不同行的记录。
从显示的图像中,例如我在datagridview
中选择了studentID 383,学生382将被更新。如果我选择了学生380,学生379将会更新,等等。
存储过程代码:
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代码
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
答案 0 :(得分:1)
因此经过一些侧面讨论后,确定从一开始看起来很有趣的联接是问题所在。学生ID与父ID不同。父信息更新必须是:
UPDATE p
SET p.father_firstname = @FFirstName,
p.father_lastname = @FLastName,
...
...
FROM parentinformation p
INNER JOIN studentinformation s
ON p.parentid = s.parentid
WHERE s.StudentID = @studID;
学生信息根本不需要加入,只需要一个简单的update where StudentID = @studID;
答案 1 :(得分:0)
至关重要的是要记住,当在网格中的数据库表中表示数据时,网格使用基于零(0)的数组来显示记录。因此,例如,如果您有一个sql数据库,其中您的主键是整数类型的表,并将其设置为自动递增1,那么当您向该表添加记录时,它们将获得沿着1行的主键, 2,3等。
现在,当您将该表格添加到网格并将最左侧的列设置为显示主键时,您最终会看到行,如上图所示,逐个递增。 HOWEVER ,这是你需要注意的一点,网格实际上编号自己的内部行集合从0开始而不是1,所以你不能只取网格的索引值#39; s行并假设它将与数据库中的主键相关,因为实际上它将由一个键出来。
因此,如果您想要更新属于主键381的信息,并且您选择该行,则其索引(网格行)实际上是380 NOT 381,请查看上面的插图。如果使用网格行索引指向数据库中的主键,然后需要向其添加1;
grid.row(I) +1
或者,您需要获取要更新的行中单元格中的值,将其转换为整数并使用该值。
同样重要的是要意识到这也会对你可能做的任何网格行循环产生影响。