Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用循环隐藏功能区控件vb.net_Vb.net_Loops - Fatal编程技术网

使用循环隐藏功能区控件vb.net

使用循环隐藏功能区控件vb.net,vb.net,loops,Vb.net,Loops,我有一个用于用户屏幕的表格 UserID-ScreenID-Perm 当表单以UserID和ScreenID打开时,我需要隐藏控件,并且我不能使用带条件的循环 这是我的代码: Try Dim da As New SqlDataAdapter Dim ds As New DataSet If dbconnect.State = ConnectionState.Closed Then dbconnect.Open() da = New SqlDataAdapter("

我有一个用于用户屏幕的表格

UserID-ScreenID-Perm

当表单以UserID和ScreenID打开时,我需要隐藏控件,并且我不能使用带条件的循环

这是我的代码:

Try
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet
    If dbconnect.State = ConnectionState.Closed Then dbconnect.Open()
    da = New SqlDataAdapter("SELECT * FROM UserScreens WHERE UserID='" & UserID & "'", dbconnect)
    da.Fill(ds, "Table")
    If ds.Tables(0).Rows.Count > 0 Then
        Dim M As DataRow = ds.Tables(0).Rows(0)
        For Each ctrl As Control In Ribbon1.Controls
            ctrl.Visible = M.Item("Perm")
        Next
        'DocIDtxt.Text = M.Item("DOCID")
    End If
Catch ex As Exception
    If dbconnect.State = ConnectionState.Open Then dbconnect.Close()
    MsgBox(ex.ToString)
End Try

我总是先担心关系。来自MS docs

与select命令关联的IDbConnection对象必须有效,但不需要打开。如果IDbConnection在调用Fill之前关闭,则打开它以检索数据,然后关闭。如果连接在调用Fill之前打开,则保持打开状态

这意味着您在.Fill方法之前打开了连接,因此它将保持打开状态。糟糕!代码关闭连接的唯一方法是出现错误。最后一个部分是。在您的Try中关闭…End Try可以达到目的,但最好使用…End-use。此块将确保即使出现错误,您的对象也已正确关闭和处置。当对象具有Dispose方法时,它可能具有必须清理的非托管资源

接下来,启用选项Strict。这将有助于指出潜在的运行时错误

第三,始终使用参数

请参阅内联解释和注释

Private Sub OpCode()
        Try
            'A DataTable does not have a Dispose() method so it will fall
            'out of scope and be garbage collected.
            Dim dt As New DataTable
            'SqlConnection has a Dispose() method so use Using
            Using cn As New SqlConnection("Your connection string")
                'SqlCommand has a Dispose() method so use Using
                Using cmd As New SqlCommand("SELECT * FROM UserScreens WHERE UserID= @UserID", cn)
                    'Always use Parameters. Never concatenate strings in SQL statements.
                    cmd.Parameters.Add("@UserID", SqlDbType.VarChar).Value = UserID
                    cn.Open()
                    'SqlDataReader has a Dispose() method so use Using
                    Using dr As SqlDataReader = cmd.ExecuteReader
                        dt.Load(dr)
                    End Using
                End Using
            End Using
            If dt.Rows.Count > 0 Then
                Dim M As DataRow = dt.Rows(0)
                For Each ctrl As Control In Ribbon1.Controls
                    'If Perm is not a boolean this line of code can't work
                    ctrl.Visible = CBool(M.Item("Perm"))
                Next
                'DocIDtxt.Text = M.Item("DOCID")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Perm是一个布尔值吗?