【问题标题】:How to use datagridview cell for a WHERE clause statement in vb.net如何在 vb.net 中将 datagridview 单元格用于 WHERE 子句语句
【发布时间】:2018-02-28 11:18:39
【问题描述】:

我在 vb.net 中有这个 sql 查询,我必须使用 datagridview 单元格作为 SELECT 查询的参数。

代码如下:

Private Sub ClassSchedule()
        Dim strConn As String = My.Settings.SLCBRegistrarDBConnectionString
        Dim sqlCon As SqlConnection = New SqlConnection(strConn)
        Try
            sqlCon.Open()
            Dim QUERY As String
            QUERY = "SELECT CSchedClass.SCode as Code, ListofSubjects.[Course No.], ListofSubjects.[Descriptive Title], CSchedSubTD.TimeAndDay as Schedule, UtlyRoom.RoomName as Room, CSchedSubInstructor.NameInit as Instructor, CSchedSubDept.Dept, CSchedSubDept.Department " &
                    "FROM CSchedMAIN INNER JOIN CSchedClass ON CSchedMAIN.SubjCode = CSchedClass.id INNER JOIN  ListofSubjects ON CSchedClass.Subj = ListofSubjects.SubjectID INNER JOIN CSchedSubTD ON CSchedMAIN.TDCode = CSchedSubTD.TDCode INNER JOIN UtlyRoom ON CSchedMAIN.RoomID = UtlyRoom.RoomID INNER JOIN CSchedSubInstructor ON CSchedMAIN.InstID = CSchedSubInstructor.EmpID INNER JOIN  CSchedSubDept ON CSchedMAIN.Dept = CSchedSubDept.DeptID INNER JOIN SemesterList ON CSchedMAIN.SemID = SemesterList.SemID INNER JOIN SchoolYear ON CSchedMAIN.SYID = SchoolYear.[SY ID] INNER JOIN CSchedSubSect ON CSchedMAIN.Section = CSchedSubSect.id " &
                    "WHERE (CSchedSubSect.Section = '" & dgvSections.SelectedCells & "') AND  (SchoolYear.[School Year] = '" & cmbSY.Text & "') AND (SemesterList.Description = '" & cmbSemester.Text & "')"
            CMD = New SqlCommand(QUERY, sqlCon)
            CMD.ExecuteNonQuery()
            Adapter = New SqlDataAdapter(CMD)
            DT = New DataTable()
            Adapter.Fill(DT)
            dgvSchedule.DataSource = DT
            dgvSchedule.Columns(0).Width = 80
            dgvSchedule.Columns(1).Width = 120
            dgvSchedule.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            dgvSchedule.Columns(3).Width = 150
            dgvSchedule.Columns(4).Width = 80
            dgvSchedule.Columns(5).Width = 150
            dgvSchedule.Columns(6).Visible = False
            dgvSchedule.Columns(7).Visible = False
            sqlCon.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

问题是下面的语法不正确:

WHERE (CSchedSubSect.Section = '" & dgvSections.SelectedCells & "')

目标是当我单击datagridview“dgvSections”中的特定单元格时,上述查询应执行并加载datagridview“dgvSchedule”中的数据。

请帮忙!

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您需要参数化您的查询。

    通过您使用SelectedCells 属性,我假设您也想检查多个值。如果是这种情况,那么您还需要将 SQL 语句从等号运算符更改为 IN operator,这在逻辑上等同于对多个值使用 OR。否则,如果您只想检查单个值,则需要从使用 SelectedCells 更改为 CurrentCell

    这里有一个简单的例子来说明我的建议:

    'Declare the object that will bind the DataGridView
    Dim dt As DataTable = New DataTable
    
    'Declare the connection object
    Dim con As SqlConnection
    
    'Wrap code in Try/Catch
    Try
        'Set the connection object to a new instance
        'TODO: Change "My Connection String Here" with a valid connection string
        con = New SqlConnection("My Connection String Here")
    
        'Create a new instance of the command object
        Using cmd As SqlCommand = New SqlCommand("SELECT
                                                    cschedclass.scode AS code,
                                                    listofsubjects.[Course No.],
                                                    listofsubjects.[Descriptive Title],
                                                    cschedsubtd.timeandday AS schedule,
                                                    utlyroom.roomname AS room,
                                                    cschedsubinstructor.nameinit AS instructor,
                                                    cschedsubdept.dept,
                                                    cschedsubdept.department
                                                FROM cschedmain
                                                INNER JOIN cschedclass
                                                    ON cschedmain.subjcode = cschedclass.id
                                                INNER JOIN listofsubjects
                                                    ON cschedclass.subj = listofsubjects.subjectid
                                                INNER JOIN cschedsubtd
                                                    ON cschedmain.tdcode = cschedsubtd.tdcode
                                                INNER JOIN utlyroom
                                                    ON cschedmain.roomid = utlyroom.roomid
                                                INNER JOIN cschedsubinstructor
                                                    ON cschedmain.instid = cschedsubinstructor.empid
                                                INNER JOIN cschedsubdept
                                                    ON cschedmain.dept = cschedsubdept.deptid
                                                INNER JOIN semesterlist
                                                    ON cschedmain.semid = semesterlist.semid
                                                INNER JOIN schoolyear
                                                    ON cschedmain.syid = schoolyear.[SY ID]
                                                INNER JOIN cschedsubsect
                                                    ON cschedmain.section = cschedsubsect.id
                                                WHERE 
                                                    cschedsubsect.section    = @section AND
                                                    schoolyear.[School Year] = @year AND
                                                    semesterlist.description = @semester;", con)
    
            'Parameterize the query
            With cmd.Parameters
                .AddWithValue("@section", dgvSections.CurrentCell.Value.ToString)
                .AddWithValue("@year", cmbSY.Text)
                .AddWithValue("@semester", cmbSemester.Text)
            End With
    
            'Create a DataAdapter to fill the underlying DataTable with data
            Using adapter As SqlDataAdapter = New SqlDataAdapter(cmd)
                'Open the connection
                con.Open()
    
                'Use the Fill method to complete the operation
                adapter.Fill(dt)
    
                'Close the connection
                con.Close()
            End Using
        End Using
    Catch ex As Exception
        'Display the error
        Console.WriteLine(ex.Message)
    Finally
        'Check if the connection object was initialized
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then
                'Close the connection if it was left open(exception thrown)
                con.Close()
            End If
    
            'Dispose of the connection object
            con.Dispose()
        End If
    End Try
    
    'Bind the table to the DataGridView
    dgvSchedule.DataSource = dt
    

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多