Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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/vb.net/17.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
.net 为什么要创建另一个SQLiteDataReader而不是重复使用?_.net_Vb.net_Sqlite - Fatal编程技术网

.net 为什么要创建另一个SQLiteDataReader而不是重复使用?

.net 为什么要创建另一个SQLiteDataReader而不是重复使用?,.net,vb.net,sqlite,.net,Vb.net,Sqlite,我搜索两个表,每个表上有一个字段 Using conn As New SQLiteConnection(SQLiteConnStr) Try conn.Open() Dim sql = "SELECT * FROM NamesTable" Dim cmd As SQLiteCommand = New SQLiteCommand(sql, conn) Dim reader As SQLiteDataR

我搜索两个表,每个表上有一个字段

Using conn As New SQLiteConnection(SQLiteConnStr)
    Try
        conn.Open()
            Dim sql = "SELECT * FROM NamesTable"
            Dim cmd As SQLiteCommand = New SQLiteCommand(sql, conn)

            Dim reader As SQLiteDataReader = cmd.ExecuteReader()
                Try
                    While (reader.Read())
                        ComboBox.Items.Add(reader("Name"))
                    End While
                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try

            Dim sql = "SELECT * FROM CityTable"
            Dim reader2 As SQLiteDataReader = cmd.ExecuteReader()
                Try
                    While (reader2.Read())
                        ComboBox.Items.Add(reader2("City"))
                    End While
                Catch ex As Exception
                    MsgBox(ex.ToString())
                End Try

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try

End Using

是因为我搜索了两个不同的表,我需要创建另一个SQLiteDataReader,而不能只重用第一个表?

您没有创建新命令,因此
cmd
变量仍然引用原始命令

您需要一行新的
cmd=newsqlitecommand(sql,conn)
,这样您就可以得到一个全新的命令


您没有重用读卡器或命令-这是一个新命令和一个新读卡器,恰好与原始的变量名相同,但它们是全新的对象。

您没有创建新命令,因此
cmd
变量仍然引用原始命令

您需要一行新的
cmd=newsqlitecommand(sql,conn)
,这样您就可以得到一个全新的命令


您没有重用读卡器或命令-这是一个新命令和一个新读卡器,只是碰巧与原始的变量名相同,但它们是全新的对象。

考虑一下查询是什么:从特定表中读取某些数据的指令。用于执行该任务的
DbCommand
DbDataReader
都是特定于该任务的,它们几乎没有可重用的功能

实际上不能创建
DbDataReader
——它是由
DbCommand
对象为您创建的(没有公共构造函数)。它非常特定于正在运行的查询,无法使用新的SQL命令重新启动它

也就是说,您可以通过不重复自己来缩短代码:

私有函数BuildDataTableForMe(sql作为字符串)作为DataTable
将conn用作新的SQLiteConnection(SQLiteConnStr)
使用cmd作为新的SQLiteCommand(sql,conn)
康涅狄格州公开赛
Dim dt作为新数据表()
dt.Load(cmd.ExecuteReader())
返回dt
终端使用
终端使用
端函数
用法:

“如果您不需要所有东西,请不要使用SELECT*
cboCity.DataSource=BuildDataTableForMe(“从CityTable中选择Id、城市”)
' ??
cboCity.DisplayMember=“城市”
cboCity.ValueMember=“Id”
cboName.DataSource=BuildDataTableForMe(“从名称表中选择Id、名称”)
'??
cboName.DisplayMember=“名称”
cboName.ValueMember=“Id”

考虑什么是查询:从特定表中读取某些数据的指令。用于执行该任务的
DbCommand
DbDataReader
都是特定于该任务的,它们几乎没有可重用的功能

实际上不能创建
DbDataReader
——它是由
DbCommand
对象为您创建的(没有公共构造函数)。它非常特定于正在运行的查询,无法使用新的SQL命令重新启动它

也就是说,您可以通过不重复自己来缩短代码:

私有函数BuildDataTableForMe(sql作为字符串)作为DataTable
将conn用作新的SQLiteConnection(SQLiteConnStr)
使用cmd作为新的SQLiteCommand(sql,conn)
康涅狄格州公开赛
Dim dt作为新数据表()
dt.Load(cmd.ExecuteReader())
返回dt
终端使用
终端使用
端函数
用法:

“如果您不需要所有东西,请不要使用SELECT*
cboCity.DataSource=BuildDataTableForMe(“从CityTable中选择Id、城市”)
' ??
cboCity.DisplayMember=“城市”
cboCity.ValueMember=“Id”
cboName.DataSource=BuildDataTableForMe(“从名称表中选择Id、名称”)
'??
cboName.DisplayMember=“名称”
cboName.ValueMember=“Id”

“您没有重用读卡器或命令”对,这就是问题所在,如果我尝试在第二次搜索中使用相同的SQLiteDataReader对象(本例中的读卡器),我会得到一个错误。如果我理解您的答案,使用相同的SQLiteCommand,我不能多次使用相同的SQLiteDataReader?”并且您没有重用该读取器或命令“对,这是问题所在,如果我尝试在第二次搜索中使用相同的SQLiteDataReader对象JETC(本例中的读取器),我会得到一个错误。如果我理解您的答案,使用相同的SQLiteCommand,我不能多次使用相同的SQLiteDataReader?谢谢您的回复。我想我读过,Try-block-Catch调用过程或外部函数是不合适的。(将代码保留在测试范围内)。现在我看到你可以调用一个函数,例如。谢谢你的回复。我想我读过,Try-block-Catch调用过程或外部函数是不合适的。(将代码保留在测试范围内)。现在我看到你可以调用一个函数,例如,你有两个很好的答案,请考虑接受一个,这样它就从未被回答的列表中移开。你有两个很好的答案,请考虑接受一个,这样它就从未被回答的列表中移开。