0

假设我有这个代码片段

Dim tab As String = "myTab"
Dim val As String = "field1"
Dim con As String = "...."
Dim qry As String 'should be: "Select * from " & tab & " where value = '" & val & "'"
Dim com As New OracleCommand (qry, con)...

还假设从数据库中检索查询字符串(即 qry 的值)。我不能将 qry 传递给 oracle 命令,因为它不会被评估,即变量没有绑定到它们的值,并且字符串按字面意思传递给命令,从而导致错误。有没有办法评估字符串并将其作为 VB 语句执行?

4

1 回答 1

1

正如@muffi 所说,使用参数非常简单。

在我的头顶上:

    Dim tab As String = "myTab"
    Dim val As String = "field1"

    Using cmd As New OracleCommand("SELECT * FROM :table WHERE value = :value", con)
        cmd.Parameters.Add(New OracleParameter() {Direction = ParameterDirection.Input, ParameterName = "table", OracleDbType = OracleDbType.Varchar2, Value = tab})
        cmd.Parameters.Add(New OracleParameter() {Direction = ParameterDirection.Input, ParameterName = "value", OracleDbType = OracleDbType.Varchar2, Value = val})
        Using OracleDataReader reader As cmd.ExecuteReader()
            While reader.Read()
                Console.WriteLine(reader.GetString(0))
            End While
        End Using
    End Using
于 2017-11-07T12:02:45.573 回答