简体   繁体   English

更改 vb.net 中 datagridview 中 ms 访问数据库中选定记录的值

[英]change value of selected records from ms access database in datagridview in vb.net

I'm working on attendance management system project in vb.net and I'm adding leave feature in it.我正在 vb.net 中开发考勤管理系统项目,并在其中添加休假功能。 I have a form in which teacher can add attendance datewise to a db and a leave form where teacher can add the student roll no, course and the date to and from which he will be on leave.我有一个表格,老师可以在其中将出勤日期按日期添加到数据库和休假表格中,老师可以在其中添加学生卷号,课程以及他将休假的日期。 This leave database should be used to change the status of a child to L.此休假数据库应用于将孩子的状态更改为 L。

This is leave table这是请假表

This will change status of all the records这将改变所有记录的状态

This is to change individual records这是为了更改个人记录

The source code源代码

Imports System.Data.OleDb导入 System.Data.OleDb

Public Class Attendance公众号 Class 考勤

Dim con As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Attendance.accdb")

Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Students.accdb")

Dim con2 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\AMS\Users.accdb")

Private Sub Attendance_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    con.Open()
    Dim cmd As New OleDbCommand("Select attend_date From " + ComboBox1.SelectedValue + " where attend_date = @date1", con)
    cmd.Parameters.AddWithValue("date1", DateTimePicker1.Value.Date)
        Dim myreader As OleDbDataReader = cmd.ExecuteReader
    If myreader.Read() Then
        con.Close()
        MessageBox.Show("Date and course Inserted Before")
    Else
        con.Close()
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim cmd2 As New OleDbCommand("Insert into " + ComboBox1.SelectedValue + " (stud_roll,stud_name,course_code,attend_date,status) Values(@roll,@name,@course,@date1,@status1)", con)
            cmd2.Parameters.AddWithValue("roll", row.Cells("StudRollDataGridViewTextBoxColumn").Value)
            cmd2.Parameters.AddWithValue("name", row.Cells("StudNameDataGridViewTextBoxColumn").Value)
            cmd2.Parameters.AddWithValue("course", ComboBox1.SelectedValue)
            cmd2.Parameters.AddWithValue("date1", DateTimePicker1.Value.ToShortDateString)
            cmd2.Parameters.AddWithValue("status1", row.Cells("status").Value)
            con.Open()
            cmd2.ExecuteNonQuery()
            con.Close()
        Next
        MessageBox.Show("Records inserted successfully")
        Dim i As Integer
        Dim count As Integer
        For i = 0 To DataGridView1.Rows.Count - 1
            If DataGridView1.Rows(i).Cells(2).Value = "P" Then
                count += 1
            End If
        Next
        Label3.Text = count
    End If
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    con1.Open()
    Dim tblname = DirectCast(ComboBox1.SelectedItem, DataRowView).Row.Field(Of String)("course_code")
    Dim cmd3 As New OleDbCommand("Select stud_roll, stud_name From [" & tblname & "]", con1)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd3
    Dim dt As New DataTable
    dt.Clear()
    da.Fill(dt)
    DataGridView1.DataSource = dt
    con1.Close()
End Sub

Private Sub Panel2_Paint(sender As Object, e As PaintEventArgs) Handles Panel2.Click

End Sub

Private Sub Panel4_Click(sender As Object, e As PaintEventArgs) Handles Panel4.Click

End Sub

Private Sub Label7_Click(sender As Object, e As EventArgs) Handles Label7.Click
    completeattendace.Show()
End Sub

Private Sub Label8_Click(sender As Object, e As EventArgs) Handles Label8.Click
    GetAttendance.Show()
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged      //this is from where the staus will change in all reacords
    con.Open()
    Dim cmd As New OleDbCommand("Select roll_no from leave where #" & DateTimePicker1.Value.Date & "# between from_date and to_date", con) // the command I'm trying to develop so that the roll no of the student is selected and used to compare and change status from "P" to "L" if the date of attendance is present between from_date and to_date
    Dim i As Integer
    If ComboBox2.SelectedItem = "--All Present--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "P"
        Next
    End If
    If ComboBox2.SelectedItem = "--All Absent--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "A"
        Next
    End If
    If ComboBox2.SelectedItem = "--Holiday--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "H"
        Next
    End If
    If ComboBox2.SelectedItem = "--Sunday--" Then
        For i = 0 To DataGridView1.Rows.Count - 1
            DataGridView1.Rows(i).Cells(2).Value = "S"
        Next
    End If
End Sub

Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
    con2.Open()
    Dim cmd As New OleDbCommand("Select course_code From courses Where semester= " + ComboBox3.SelectedItem + "", con2)
    Dim da As New OleDbDataAdapter
    da.SelectCommand = cmd
    Dim dt As New DataTable
    dt.Clear()
    da.Fill(dt)
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "course_code"
    ComboBox1.ValueMember = "course_code"
    con2.Close()
End Sub

Private Sub Label9_Click(sender As Object, e As EventArgs) Handles Label9.Click

End Sub

Private Sub Label10_Click(sender As Object, e As EventArgs)

End Sub

End Class结束 Class

Is there any command or something through which I can check if a student roll number in database is in the course I'm adding attendance of only if the date(of datetimepicker1) on which attendance will be taken is between the leave to and from dates in database and change the status of only that student to "L" till the leave date ends.是否有任何命令或其他东西可以检查数据库中的学生卷号是否在课程中在数据库中并将只有该学生的状态更改为“L”,直到休假日期结束。 Please anyone help me, my college professor doesnt know how to do it but wants me to do it.请任何人帮助我,我的大学教授不知道该怎么做,但希望我去做。

You doun't expose the database structure (as required in Stackoverflw for SQL questions), so my answer is shown on some generic table and field names.您没有公开数据库结构(如 Stackoverflw 中要求的 SQL 问题),所以我的答案显示在一些通用表和字段名称上。 Also, I'm not sure I clearly understand your question.另外,我不确定我是否清楚地理解了您的问题。

To find all students that have roll_number from a particular course (assumes tables Courses, Students and StudentErols):要从特定课程中查找所有拥有roll_number的学生(假设表 Courses、Students 和 StudentErols):

SELECT ST.LastName, ST.FirstName
FROM Couses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & ";

To check if particular student has roll_number in a particular course, see if you get any results from this modified query:要检查特定学生在特定课程中是否有roll_number ,请查看您是否从修改后的查询中获得任何结果:

SELECT ST.LastName, ST.FirstName
FROM Couses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & "
  AND ST.ID = " & Me.StudentID & "

To create attendance of all students who are enrolled in a particular course (assumes tables Courses, Students and Attendance):要创建注册特定课程的所有学生的出勤率(假设表格课程、学生和出勤率):

INSERT INTO Attendance as ATT (StudentID, CourseID)
SELECT ST.ID, CRS.ID
FROM Courses as CRS
LEFT JOIN StudentEntrols as SE on SE.CourseID = CRS.ID
LEFT JOIN Students as ST ON ST.ID = SE.StudentID 
WHERE CRS.ID = " & Me.CourseID & ";

You can modify that to your database structure and add parameters (which are probably overkill, considering it's just Access, run probably by a single staff).您可以将其修改为您的数据库结构并添加参数(这可能是矫枉过正,考虑到它只是 Access,可能由一个工作人员运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM