问:SQL:INSERT / UPDATE如何将COUNT的值插入表中?

时间:2015-12-23 02:40:48

标签: sql count oledb

我想计算field1中的所有(1)并将计数值插入另一个表中。 我是编程新手,我无法理解错误。

    If Not cnn.State = ConnectionState.Open Then
        cnn.Open()
    End If
    Dim strSQL As String = "SELECT COUNT(*) FROM VOTED WHERE field1 = 1" 'Create new connection'
    Dim CMD As New OleDbCommand(strSQL, cnn) 'Create new command'
    Dim DR As OleDbDataReader
    DR = CMD.ExecuteReader() 'Populate the reader'
    While (DR.Read())
        Dim cmd1 As New OleDbCommand
        cmd1.connection = cnn
        cmd1.CommandText = "UPDATE CandidateDB " & _
                            " SET VoteCount=" & DR(0).ToString() & _
                            " WHERE CandidateID=" & 1
        cmd1.ExecuteNonQuery()
        'MessageBox.Show(DR(0).ToString()) 'Show the result of the query'

ExecuteNonQuery:没有给出一个或多个必需参数的值。

我试过

executioncalar:没有给出一个或多个必需参数的值。

1 个答案:

答案 0 :(得分:0)

就像错误消息所示,您必须在调用ExecuteNonQuery之前在命令对象cmd1上设置Connection属性:

cmd1.Connection = cnn