使用vb.net中的存储过程更新表

时间:2017-07-21 01:43:24

标签: sql vb.net sql-server-2012

我在如何使用datagridview和存储过程更新我的表时遇到问题。这是代码;

Private Sub INPUTGrades()
    Dim strConn As String = "Data Source=Jansen;Initial Catalog=SLCBRegistrarDB;Integrated Security=True"
    Dim sqlCon As SqlConnection = New SqlConnection(strConn)

    CMD = New SqlCommand
    CMD.Connection = sqlCon
    CMD.CommandText = "PostingofGRADE"
    CMD.CommandType = CommandType.StoredProcedure

    CMD.Parameters.AddWithValue("@AcademicYear", cmbAcademicYear.Text)
    CMD.Parameters.AddWithValue("@Period", cmbPeriod.Text)
    CMD.Parameters.AddWithValue("@Section", cmbSection.Text)
    CMD.Parameters.AddWithValue("@CourseCode", cmbSubjectCode.Text)
    CMD.Parameters.AddWithValue("@DescriptiveTitle", cmbDescription.Text)
    CMD.Parameters.AddWithValue("@AcademicLevel", cmbAcadLevel.Text)

    CMD.Parameters.Add(New SqlParameter("@StudentID", SqlDbType.Int))
    CMD.Parameters.Add(New SqlParameter("@Grade", SqlDbType.NVarChar))

    sqlCon.Open()
    For Each row As DataGridViewRow In dgvSubjectsEntry.Rows
        If Not row.IsNewRow Then
            CMD.Parameters("@StudentID").Value = row.Cells(1).Value
            CMD.Parameters("@Grade").Value = row.Cells(0).Value

            Dim RowsAffected As Integer = CMD.ExecuteNonQuery()
            If RowsAffected > 0 Then
                MsgBox("Grade Successfully Posted.")
            Else
                MsgBox("Failed!")
            End If
        End If
    Next
    sqlCon.Close()
End Sub

这是存储过程:

USE [SLCBRegistrarDB]
GO
/****** Object:  StoredProcedure [dbo].[PostingofGRADE]    Script Date: 
7/21/2017 9:38:16 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[PostingofGRADE]

@StudentID Int,
@AcademicYear nvarchar(50),
@Period nvarchar(50),
@Section nvarchar(10),
@CourseCode nvarchar(50),
@DescriptiveTitle nvarchar(100),
@Grade nvarchar(50),
@AcademicLevel nvarchar(50)

AS
BEGIN

    Declare @AcademicLevelID int
    Declare @SchoolID int
    Declare @TypeofStudentID int
    Declare @CourseID int
    Declare @PeriodID int
    Declare @AcademicYearID int
    Declare @SectionID int
    Declare @SubjectCode int

        SELECT @AcademicLevelID=AcadLevelID FROM [Academic Level] WHERE 
[Academic Level]=@AcademicLevel

        SELECT @PeriodID=SemID FROM SemesterList WHERE Description=@Period

        SELECT @SectionID=SectionID FROM Section WHERE Section=@Section

        SELECT @AcademicYearID=[SY ID] FROM SchoolYear WHERE [School Year]=@AcademicYear

 SELECT @SubjectCode=StudentAcademicRecords.[Subject Code]
 FROM         StudentAcademicRecords INNER JOIN
         [Class Schedule LINE] ON StudentAcademicRecords.[Subject Code] = 
 [Class Schedule LINE].SchedID INNER JOIN
         Curriculum ON [Class Schedule LINE].[Subject Code] = Curriculum.
[Subject Code] INNER JOIN
         ListofSubjects ON Curriculum.SubjectID = ListofSubjects.SubjectID 
 INNER JOIN
         SchoolYear ON StudentAcademicRecords.[Academic Year] = SchoolYear.
 [SY ID] INNER JOIN
         SemesterList ON StudentAcademicRecords.Period = SemesterList.SemID 
AND Curriculum.SemesterID = SemesterList.SemID
WHERE        (ListofSubjects.[Course No.] = @CourseCode) AND 
(ListofSubjects.[Descriptive Title] = @DescriptiveTitle) AND 
         (StudentAcademicRecords.StudentID = @StudentID) AND 
(SemesterList.Description = @Period) AND (SchoolYear.[School Year] = 
@AcademicYear)

BEGIN
    UPDATE StudentAcademicRecords
    SET StudentID =@StudentID, [Academic Level] =@AcademicLevelID, Period 
=@PeriodID, [Academic Year] =@AcademicYearID, Section =@SectionID, [Subject 
Code] =@SubjectCode, Grade =@Grade
    FROM StudentAcademicRecords
    WHERE [Subject Code]=@SubjectCode AND StudentID=@StudentID
END

END 

如果我尝试运行代码,表格没有任何反应。根本没有变化。我尝试在sql server查询中运行,它工作正常,但在我的vb.net代码中,它没有。

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

我通常所做的并不完全像你所拥有的那样,但它对我有用,也许它会为你工作,我使用UPSERT参数化查询,本质上它会检查现有的数据库表查看要插入的密钥是否与任何现有密钥匹配,如果是,则执行更新,如果没有,则将新数据作为新行插入,这是我在代码中使用的循环UPSERT查询的示例:

    Dim Connexion As SqlConnection = New SqlConnection(dbLocations(0, 1))
        Dim updateStatement As String = Nothing
        Dim updatecommand As New SqlCommand(updateStatement, Connexion)
        Try
            Connexion.Open()
            query = String.Empty
            query &= "UPDATE schedule SET Task = @Task, Complete = @Complete, Start_date = @Start_date, "
            query &= "Due_date = @Due_date, JRID = @JRID, Task_Manager = @Task_Manager, Entered_By = @Entered_By, Time_Entered = @Time_Entered "
            query &= "WHERE TaskID = @TaskID "
            query &= "IF @@ROWCOUNT = 0 INSERT INTO schedule ( TaskID, Task, start_date, Due_Date, Complete, Task_Manager, JRID, Entered_By, Time_Entered)"
            query &= " VALUES ( @TaskID, @Task, @start_date, @Due_Date, @Complete, @Task_Manager, @JRID, @Entered_By, @Time_Entered);"

            updatecommand.CommandText = query

            If MainSchedule.isokclicked = 1 Then
                For Each row As DataGridViewRow In MainSchedule.DataGridView1.Rows
                    If Not (row.Cells(0).Value = Nothing) Then
                        insertcommand.Parameters.Clear()
                        insertcommand.CommandText = query
                        insertcommand.Parameters.AddWithValue("@TaskID", row.Cells(0).Value)
                        insertcommand.Parameters.AddWithValue("@Complete", "False")
                        insertcommand.Parameters.AddWithValue("@Task", row.Cells(1).Value)
                        insertcommand.Parameters.AddWithValue("@Start_date", row.Cells(2).Value)
                        insertcommand.Parameters.AddWithValue("@Due_Date", row.Cells(3).Value)
                        insertcommand.Parameters.AddWithValue("@JRID", txtJRID.Text)
                        insertcommand.Parameters.AddWithValue("@Task_Manager", row.Cells(4).Value)
                        insertcommand.Parameters.AddWithValue("@Entered_By", GetUserName())
                        insertcommand.Parameters.AddWithValue("@Time_Entered", Now)
                        insertcommand.ExecuteNonQuery()
                    End If
                    keypos = keypos + 1
                Next
                Connexion.Close()
            Else
            End If

答案 1 :(得分:0)

Ow I' m很抱歉伙计们......我的代码是正确的..它工作得很完美..我刚刚在我的Windows窗体中的组合框中插入一个错误的条目,这使得SQL代码中的SELECT语句返回0 ..它工作得很好..我很抱歉,这太可惜了。

"说明"也应该用" [Sem。]"代替。我只是指定了错误的列名。太多的争论扭曲了我的想法。对不起大家。这是一个很好的代码。