简体   繁体   English

ASPX VB.Net OleDb将参数插入查询

[英]ASPX VB.Net OleDb Insert Parameter into Query

This is my first time writing in VB.Net for aspx pages. 这是我第一次在VB.Net中编写aspx页面。

The problem I having is that the parameter is not going into the query at the line for cmd.Parameters.Add . 我遇到的问题是该参数没有进入cmd.Parameters.Add行中的查询。

The error I am getting is 我得到的错误是

No value given for one or more required parameters. 没有为一个或多个必需参数给出值。

on the line 在线上

reader = cmd.ExecuteReader;

I have tried: 我努力了:

  • Adding the PARAMETERS at the top of the query like I have shown; 如我所示,将PARAMETERS添加到查询的顶部;
  • Removing and Adding [] around the parameter; 删除并在参数周围添加[]
  • Changing OleDbType.Integer to OleDbType.SmallInt or OleDbType.BigInt OleDbType.Integer更改为OleDbType.SmallIntOleDbType.BigInt

I know the query works as I can place it into MS Access and will run once I add the parameter. 我知道查询可以正常工作,因为我可以将其放入MS Access,并且在添加参数后即可运行。 But not when I run it in Visual Studio. 但是当我在Visual Studio中运行它时却没有。

Dim reader As OleDbDataReader
Dim cmd As OleDbCommand
Dim SQL As String = "PARAMETERS [@ID] Long; " &
                    "SELECT tblField.FieldName, " &
                           "tblField.FieldCaption, " &
                           "tblField.FieldMinCharNum, " &
                           "tblField.FieldMaxCharNum, " &
                           "tblField.FieldDefault, " &
                           "tblField.FieldSection, " &
                           "tblField.FirstQuestion, " &
                           "tblField.FieldDescription, " &
                           "tblField.FieldRegEx " &
                     "FROM tblField " &
                     "WHERE tblField.FieldID = [@ID];"

cmd = New OleDbCommand(SQL, Connection.Connection)
cmd.Parameters.Add("[@ID]", OleDbType.Integer).Value = ID

reader = cmd.ExecuteReader

I have a work around to make it work by just pre-inserting the parameter into the SQL string. 我只是通过将参数预先插入到SQL字符串中来使其工作。 But I want to make this work for other areas of the page that are yet to be written. 但我想使此功能适用于页面上尚未编写的其他区域。 Where user inputs are coming back into database so inputs are sanitised. 用户输入返回到数据库中,以便对输入进行清理。

Thanks to everyone. 谢谢大家。

OLEDB doesn't use @ to identify parameters. OLEDB不使用@来标识参数。 It uses ? 它用 ? and allocates parameters in the order they appear in the SQL amend your code to... 并按照它们在SQL中出现的顺序分配参数,将您的代码修改为...

Dim reader As OleDbDataReader
Dim cmd As OleDbCommand
Dim SQL As String = "SELECT tblField.FieldName, " &
                           "tblField.FieldCaption, " &
                           "tblField.FieldMinCharNum, " &
                           "tblField.FieldMaxCharNum, " &
                           "tblField.FieldDefault, " &
                           "tblField.FieldSection, " &
                           "tblField.FirstQuestion, " &
                           "tblField.FieldDescription, " &
                           "tblField.FieldRegEx " &
                      "FROM tblField " &
                     "WHERE tblField.FieldID = ?"

cmd = New OleDbCommand(SQL, Connection.Connection)
cmd.Parameters.Add("?", OleDbType.Integer).Value = ID

reader = cmd.ExecuteReader

I don't understand why your mentioning SQL are you retrieving the data from SQL Query or are you going to insert data into the table. 我不明白为什么您提到的SQL是您要从SQL Query检索数据,还是要将数据插入表中。

your using Dim cmd As OleDbCommand means use to insert the input values into the database like see below sample code. 您使用Dim cmd As OleDbCommand意思是用于将输入值插入数据库,如下面的示例代码所示。

       query = "INSERT INTO ds.students (ID,NAME,PIC)" & _
       "VALUES (@ID,@NAME,@PIC);"
        Dim cmd As OracleCommand = New OracleCommand(query, con)
        cmd.Parameters.Add("@ID", Convert.ToInt32(TextBox1.Text))
        cmd.Parameters.Add("@NAME", Convert.ToString(TextBox2.Text))
        cmd.Parameters.Add("@PIC", arrImage)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()

you can try this also 你也可以尝试

Using cn As OracleConnection = New OracleConnection(connectionString)

    cn.Open()

    Using cmd As OracleCommand = New OracleCommand()

        Const sql As String = "Insert into test_table (val1, val2) values (:var1, :var2)"
        cmd.Connection = cn
        cmd.Parameters.Add(New OracleParameter("var1", TxtField1.Text))
        cmd.Parameters.Add(New OracleParameter("var2", TxtField2.Text))
        cmd.CommandText = sql
        cmd.ExecuteNonQuery()

    End Using

End Using

if you want to insert the values into the database change your code according to given samples. 如果要将值插入数据库,请根据给定的示例更改代码。

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM