Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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_Ms Access - Fatal编程技术网

Vb.net 用于删除信息的删除按钮不工作

Vb.net 用于删除信息的删除按钮不工作,vb.net,ms-access,Vb.net,Ms Access,目前,我正试图用我的解决方案从ms access中删除数据,但目前无法工作。我已经在按钮删除上列出了消息框,但它没有显示,并且在我启动它时也没有显示任何错误。代码如下: MessageBox.Show("Are You Sure You Want To Delete?", "Deletion",_ MessageBoxButtons.YesNo, MessageBoxIcon.Question) If DialogResult.Yes Then

目前,我正试图用我的解决方案从ms access中删除数据,但目前无法工作。我已经在按钮删除上列出了消息框,但它没有显示,并且在我启动它时也没有显示任何错误。代码如下:

 MessageBox.Show("Are You Sure You Want To Delete?", "Deletion",_
                 MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If DialogResult.Yes Then
        Function1()

        UserHomepage.Show()
        Me.Hide()
    ElseIf DialogResult.No Then
        Me.Show()
        UserHomepage.Hide()
        lblname.Hide()
        txtsid.ResetText()
    End If


    cmdDelete.CommandText = "Delete from student where student_id = " + txtsid.Text + ";"
    cmdDelete.CommandType = CommandType.Text
    cmdDelete.Connection = cnnOLEDB
    cmdDelete.ExecuteNonQuery()
这是用于功能1的^

  • 我们需要知道代码发生了什么事件,才能理解为什么不调用它。我把它放在一个按钮中,当点击时调用代码
  • 如果希望从消息框中获得结果,请设置一个变量来保存结果。然后根据DialogResult枚举值测试结果。在您的情况下,您只有Yes或No,因此不需要测试第二个条件(DialogResult.No),只需使用else即可
  • 我不知道Function1是什么,也不知道为什么要调用它。命名变量、函数和子对象,使它们有意义
  • 你从某个神秘的地方得到了联系。我不知道这是否是一个开放的连接(我希望不是!)
  • 使用…EndUsing语句可以确保即使出现错误,对象也会被正确关闭和释放。关闭连接非常重要
  • 可以在命令的构造函数中设置命令对象的.Text属性和.Connection属性
  • 连接字符串时,使用&而不是+。这避免了可能需要添加的代码混淆
  • 但是,决不要在SQL字符串中使用串联。您可能有来自用户的恶意输入。始终使用参数。Access不关心参数的名称。Access只关心它们的添加顺序。这里没有关系,因为我们只有一个参数
  • 要执行命令,需要打开连接
  • 尽快关闭您的连接
  • `


    你如何调用这个函数?你为你的按钮设置了OnClick事件了吗?你能解释一下@MihaiAdrian是什么意思吗?我的函数1是:cmdDelete.CommandText=“从学生中删除,其中student_id=“+txtsid.Text+”;”cmdDelete.CommandType=CommandType.Text cmdDelete.Connection=cnnOLEDB cmdDelete.ExecuteNonQuery(),我的意思是。。。您说msgBox不会出现,也不会出现错误。你确定你的代码运行吗?学生id列的数据类型是什么?数字列表和代码格式错误的解决方法是什么?如果你在代码块之前添加代码,你可以使用按钮正常格式化。
     Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
                Dim dr As DialogResult = MessageBox.Show("Are You Sure You Want To Delete?", "Deletion",
                         MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If dr = DialogResult.Yes Then
                    Using cnnOLEDB As New OleDb.OleDbConnection("Your connection string")
                        Using cmdDelete As New OleDbCommand("Delete from student where student_id = @ID;", cnnOLEDB)
                            cmdDelete.CommandType = CommandType.Text
                            cmdDelete.Parameters.Add("@ID", OleDbType.Integer).Value = txtsid.Text
                            cnnOLEDB.Open()
                            cmdDelete.ExecuteNonQuery()
                            cnnOLEDB.Close()
                        End Using
                    End Using
                    UserHomepage.Show()
                    Me.Hide()
                Else
                    Me.Show()
                    UserHomepage.Hide()
                    lblname.Hide()
                    txtsid.ResetText()
                End If
            End Sub`