Sql 为什么会发生这种错误&引用;没有给出一个或多个所需参数的值“;

Sql 为什么会发生这种错误&引用;没有给出一个或多个所需参数的值“;,sql,database,vb.net,Sql,Database,Vb.net,当我尝试单击按钮1、2和3时。发生下面所述的错误。当数据库中有两个表时,总是会发生此错误 没有为一个或多个必需参数提供值 这是我的密码 Imports System.Data.OleDb Module MSAccessConnection Public Function OpenConnection() As String Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=

当我尝试单击按钮1、2和3时。发生下面所述的错误。当数据库中有两个表时,总是会发生此错误

没有为一个或多个必需参数提供值

这是我的密码

Imports System.Data.OleDb
Module MSAccessConnection
    Public Function OpenConnection() As String
        Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=G:\MerdoresNew\OrderData.accdb"

        Return connString
    End Function
End Module


Public Class Order
    Public total As Integer
    Public ordered As Integer
    Public ordered2 As Integer

    Public ordered3 As Integer
    Public price As Integer
    Dim myConnection As OleDbConnection = New OleDbConnection


    Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'OrderDatabaseDataSet.Items' table. You can move, or remove it, as needed.
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myConnection.ConnectionString = OpenConnection()
        myConnection.Open()

        'the query:
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Cheese Burger'", myConnection)

        Dim dr As OleDbDataReader = cmd.ExecuteReader
        ' the following variable is hold true if user is found, and false if user is not found
        Dim Found As Boolean = False

        While dr.Read
            Found = True
            price = dr("Price")
        End While

        If Found = True Then


            ordered = TextBox1.Text * price



        Else





            MsgBox("Saved!")

            Me.Close()


        End If

        myConnection.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        myConnection.ConnectionString = OpenConnection()
        myConnection.Open()

        'the query:
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Ham Burger'", myConnection)

        Dim dr As OleDbDataReader = cmd.ExecuteReader
        ' the following variable is hold true if user is found, and false if user is not found
        Dim Found As Boolean = False

        While dr.Read
            Found = True
            price = dr("Price")
        End While

        If Found = True Then


            ordered2 = TextBox2.Text * price




        Else








            MsgBox("Saved!")


        End If

        myConnection.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        total = ordered + ordered2 + ordered3
        Form1.TextBox1.Text = total





    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        myConnection.ConnectionString = OpenConnection()
        myConnection.Open()

        'the query:
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Chicken Sandwich'", myConnection)

        Dim dr As OleDbDataReader = cmd.ExecuteReader
        ' the following variable is hold true if user is found, and false if user is not found
        Dim Found As Boolean = False

        While dr.Read
            Found = True
            price = dr("Price")
        End While

        If Found = True Then


            ordered3 = TextBox3.Text * price




        Else


            MsgBox("Saved!")

        End If

        myConnection.Close()
    End Sub
End Class

我看不到您将连接字符串传递给按钮单击处理程序的位置。将连接字符串暗显为connString,但从未在处理程序中调用它

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    myConnection.ConnectionString = OpenConnection() <----- Change ConnectionString to connString
    myConnection.Open()
Private子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击

myConnection.ConnectionString=OpenConnection()能否花点时间正确格式化代码?并提供简洁的代码来回答您的问题?我看到几个例程都与数据库通信。当然,一个例行程序就足以证明你的问题了?请提供一个例子。这目前是一个调试问题,但除了大量代码之外,您没有给我们提供太多的内容。请检查代码中的列和表名在数据库中是否相同。此外,如果使用,Visual Studio将为您指出一些问题,甚至建议一些修复方法。OP使用函数
OpenConnection()
获取连接字符串
connString
不在您建议使用它的范围内,尽管无论如何,
connString
不是OleDbConnection的属性。我错过了OleDbConnection部分,只是看到了SQL标记。我的错。