Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.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
Database 数据库仅在超出错误系统资源之前添加(x)行数_Database_Vb.net_Ms Access - Fatal编程技术网

Database 数据库仅在超出错误系统资源之前添加(x)行数

Database 数据库仅在超出错误系统资源之前添加(x)行数,database,vb.net,ms-access,Database,Vb.net,Ms Access,我的代码有一个问题,我只能添加这么多行文本,然后出现错误“超出了系统资源” 这是我的代码: Dim x As Integer = MsgBox("Update Record?", MsgBoxStyle.YesNo, "Are you sure?") If x = MsgBoxResult.Yes Then Dim accessconn As New _ System.Data.OleDb.OleDbConnection("Provider=Microsoft.

我的代码有一个问题,我只能添加这么多行文本,然后出现错误“超出了系统资源”

这是我的代码:

Dim x As Integer = MsgBox("Update Record?", MsgBoxStyle.YesNo, "Are you sure?")

    If x = MsgBoxResult.Yes Then
        Dim accessconn As New  _
   System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "MyDB.accdb")
        Dim com As System.Data.OleDb.OleDbCommand
        accessconn.Close()
        Try
            For Each strLine As String In TextBox1.Text.Split(vbNewLine)
                accessconn.Open()
                Dim str As String
                Dim dr As OleDbDataReader
                str = "SELECT * FROM Table4 WHERE MD5='" & strLine & "'"
                Dim cmd As OleDbCommand = New OleDbCommand(str, accessconn)
                dr = cmd.ExecuteReader
                If dr.Read() Then
                    Label2.Text = Label2.Text + 1
                Else
                    accessconn.Open()
                    com = New System.Data.OleDb.OleDbCommand("INSERT INTO Table4(MD5) VALUES('" & strLine & "')", accessconn)
                    com.ExecuteReader(CommandBehavior.CloseConnection)
                    Label3.Text = Label3.Text + 1
                    com.Dispose()
                    accessconn.Close()
                End If
            Next
            accessconn.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        MsgBox("Done")
        PopulateGridview4()
    End If

我想能够添加无限行的文字数据库,如果可能的话。请帮助。

您应该将代码更改为以下内容。注意

  • 返回对象(如
    OleDbConnection
    OleDbCommand
    OleDbDataReader
    )的所有内容都使用
    块包装在
    中。这些对象都实现了
    IDisposable
    接口,这意味着在使用完它们之后,应该尽快清理它们
  • 还要注意的是,您的
    INSERT
    没有返回任何数据,因此您应该使用
    ExecuteNonQuery
    而不是
    ExecuteReader
  • 最后,请不要养成在所有东西周围放置
    Try/Catch/endtry
    块的习惯。您正在显示异常(您显示了
    ex.ToString
    ,这是一件好事),但随后忽略了异常。一般来说,除非您能够修复异常,否则不要捕获异常
  • 代码:


    另外,如果使用
    If x MsgBoxResult,您可能会使其缩进程度稍微降低。是的,然后
    退出,但我不知道这是否在
    函数
    中,因此“退出”可能会有所不同。

    您的
    OLEDB连接
    OLEDB命令
    ,和
    OleDbReader
    都需要在
    中使用
    块来清理资源。你能给我举个例子吗,因为我刚刚使用vb.net和/或数据库。非常感谢,我今晚下班回来后会尝试一下,然后再与你联系。再次感谢。如果它不起作用,那么你应该删除复选标记!这很好,只需删除第二个accessconn.Open(),再次感谢@johnsaunderson,再问一个问题,当我在文本框中填充400000行时,这仍然有效,但速度非常慢。有没有更快的方法将大型文本文件添加到数据库中?我相信有更好的方法,但我不确定你想完成什么。此外,这将是一个单独的问题。除此之外,还要学习使用参数化查询。除此之外,您还应该对代码进行性能分析,看看它在哪里花费时间,然后让它在那里花费更少的时间。如果它经常非常慢,那么您可能需要了解T-SQL
    MERGE
    语句。您可以玩一些花哨的把戏,将一块XML连同行一起传递给一个存储过程,该存储过程将调用
    MERGE
    来插入那些尚未出现的行
    Dim x As Integer = MsgBox("Update Record?", MsgBoxStyle.YesNo, "Are you sure?")
    
    If x = MsgBoxResult.Yes Then
        Using accessconn As New  _
            System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "MyDB.accdb")
            accessconn.Open()
            For Each strLine As String In TextBox1.Text.Split(vbNewLine)
                Dim str As String = "SELECT * FROM Table4 WHERE MD5='" & strLine & "'"
                Using cmd As OleDbCommand = New OleDbCommand(str, accessconn)
                    Using dr As OleDbDataReader = cmd.ExecuteReader
                        If dr.Read() Then
                            Label2.Text = Label2.Text + 1
                        Else
                            Using com As OleDbCommand = New System.Data.OleDb.OleDbCommand("INSERT INTO Table4(MD5) VALUES('" & strLine & "')", accessconn)
                                com.ExecuteNonQuery()
                            End Using
                            Label3.Text = Label3.Text + 1
                        End If
                    End Using
                End Using
            Next
            MsgBox("Done")
            PopulateGridview4()
        End Using
    End If