Visual Basic与.NET中的多步OLE DB错误

时间:2014-07-24 03:49:48

标签: sql .net basic

这是我的第一个编程课,这个学期的主要项目,我收到了这个错误:

  

“多步OLE DB操作生成错误。检查每个OLE DB状态值,如果可用。没有工作。”

我们的代码应该提交给Access数据库,然后将信息拉回到数据网格中进行查看。我已经检查过Access类型是否设置正确(有些是整数,有些是短文本) 我不知道如何检查数据网格的“长度”,如某些帖子所述。

以下是代码:

Private Sub DisplayResults(teamname As String)

    Dim table As New DataTable
    table.Columns.Add("Team Name", GetType(String))
    table.Columns.Add("Question 1", GetType(String))
    table.Columns.Add("Question 2", GetType(String))
    table.Columns.Add("Question 3", GetType(String))
    table.Columns.Add("Question 4", GetType(String))
    table.Columns.Add("Comments", GetType(String))
    table.Columns.Add("Average", GetType(String))

    'Create a connection to the Access database using the 'main' connection string defined in web.config

    Using connection As New OleDbConnection(ConfigurationManager.ConnectionStrings("main").ConnectionString)

        ' Create a command that will get results for all teams or the specified team

        Using command As OleDbCommand = connection.CreateCommand

            If TeamNameList2 Is Nothing Then

                ' If a team name wasn't requested, just select all evaluations

                command.CommandText = "SELECT * FROM Evaluation"

            Else

                ' If a team name was selected, make sure we filter for it

                command.CommandText = "SELECT * FROM Evaluation WHERE TeamName = ?"

                command.Parameters.AddWithValue("@TeamName", TeamNameList2)

            End If

            connection.Open()

            Using reader As OleDbDataReader = command.ExecuteReader

                While reader.Read

                    ' Populate a row in the data container that will be displayed in the grid

                    Dim row As DataRow = table.NewRow

                    row("Team Name") = reader("TeamName")
                    row("Question 1") = reader("Question1")
                    row("Question 2") = reader("Question2")
                    row("Question 3") = reader("Question3")
                    row("Question 4") = reader("Question4")
                    row("Comments") = reader("Comments")
                    row("Average") = reader("Average")
                    table.Rows.Add(row)

                End While


                ' Bind the data container to the grid, which causes it to be displayed

                ReportGrid.DataSource = table

                ReportGrid.DataBind()

            End Using

        End Using

    End Using

End Sub

有人要求发布连接字符串。我不确定这是不是你的意思,但这里有一些其他代码可以更新评估:

受保护的Sub SubmitButton_Click(sender As Object,e As System.EventArgs)处理SubmitButton.Click         '使用web.config

中定义的'main'连接字符串创建与Access数据库的连接
    Using connection As New OleDbConnection(ConfigurationManager.ConnectionStrings("main").ConnectionString)

        ' Create a command that will update an evaluation for the specified team

        Using command As New OleDbCommand("UPDATE Evaluation SET Question1=?, Question2=?, Question3=?, Question4=?, Comments=?, Average=? WHERE TeamName=?", connection)
            command.Parameters.AddWithValue("@Question1", Question1.SelectedValue)
            command.Parameters.AddWithValue("@Question2", Question2.SelectedValue)
            command.Parameters.AddWithValue("@Question3", Question3.SelectedValue)
            command.Parameters.AddWithValue("@Question4", Question4.SelectedValue)
            command.Parameters.AddWithValue("@Comments", Comments1.Text)
            command.Parameters.AddWithValue("@Average", txtAverage.Text)
            command.Parameters.AddWithValue("@TeamName", Session("EvaluationTeamName"))

            connection.Open()


            ' Execute the command which will actually perform the update in the database

            command.ExecuteNonQuery()

        End Using
    End Using

    ' Clear the team name so that the evaluation cannot be accidentally taken again
    Session.Remove("EvaluationTeamName")

    Response.Redirect("~/Default.aspx")
End Sub

0 个答案:

没有答案