[英]Update table using stored procedure in vb.net
i'm having trouble on how do i update my table using datagridview and stored procedure. 我在使用datagridview和存储过程更新表方面遇到麻烦。 here is the code;
这是代码;
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
And this is the Stored Procedure: 这是存储过程:
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
If i try to run the code, nothing happens with the table. 如果我尝试运行代码,则表未执行任何操作。 no changes at all.
完全没有变化。 I tried running in the sql server query, and it works fine but in my vb.net code, it does not.
我尝试在sql服务器查询中运行,但工作正常,但在我的vb.net代码中却没有。
Any help is appreciated. 任何帮助表示赞赏。
What I normally do isn't exactly like what you have, but it works for me, and maybe it'll work for you, i use a UPSERT Parameterized query, essentially it checks the existing database table to see if the key to be inserted matches any existing and if it does, performs an update, and if not, inserts that new data as a new row, here is an example of a looped UPSERT query that I use in my code: 我通常不完全符合您的要求,但是它对我有用,也许对您有用,我使用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
Ow I'm Sorry guys..My Code is correct..it works perfectly.. i just inserted a wrong entry in combobox items in my windows form that makes the SELECT Statement in the SQL Code returns 0.. It works fine.. i'm sorry guys, that's a shame. 抱歉,我是对的..我的代码正确..它工作正常..我只是在Windows窗体的组合框中插入了一个错误的条目,使SQL代码中的SELECT语句返回0 ..它正常工作..我很抱歉,这太可惜了。
"Description" also should be replaced with "[Sem.]". “说明”也应替换为“ [Sem。]”。 I just specified the wrong column name.
我只是指定了错误的列名。 Too many arguments that twisted my mind.
太多的争论扭曲了我的想法。 Sorry guys.
对不起大家。 this is a good code.
这是一个很好的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.