错误:ExecuteNonQuery:CommandText属性尚未初始化

时间:2012-01-31 18:03:52

标签: vb.net insert executenonquery

此代码在按钮单击中,我使用溢出来获取每个数据 但我在“cmd.CommandType = CommandType.Text”

时遇到错误
 Dim conn As New SqlConnection(GetConnectionString())
            Dim sb As New StringBuilder(String.Empty)

            Dim splitItems As String() = Nothing
            For Each item As String In sc

                Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
                If item.Contains(",") Then
                    splitItems = item.Split(",".ToCharArray())
                    sb.AppendFormat("{0}('{1}'); ", sqlStatement, splitItems(0))

                End If
            Next

            Try
                conn.Open()
                Dim cmd As New SqlCommand(sb.ToString(), conn)

                cmd.CommandType = CommandType.Text
                cmd.ExecuteNonQuery()


                Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
            Catch ex As System.Data.SqlClient.SqlException
                Dim msg As String = "Insert Error:"
                msg += ex.Message

                Throw New Exception(msg)
            Finally
                conn.Close()
            End Try

相同的代码,下面的工作 Dim conn As New SqlConnection(GetConnectionString())         Dim sb As New StringBuilder(String.Empty)

    Dim splitItems As String() = Nothing
    For Each item As String In sc

        Const sqlStatement As String = "INSERT INTO GuestList (groupID,guest,contact,eEmail,relationship,info,customerID) VALUES"
        If item.Contains(",") Then
            splitItems = item.Split(",".ToCharArray())
            sb.AppendFormat("{0}('{1}','{2}','{3}','{4}','{5}','{6}','{7}'); ", sqlStatement, splitItems(0), splitItems(1), splitItems(2), splitItems(3), splitItems(4), splitItems(5), Session("customerID"))

        End If
    Next

    Try
        conn.Open()
        Dim cmd As New SqlCommand(sb.ToString(), conn)
        cmd.CommandType = CommandType.Text
        cmd.ExecuteNonQuery()


        Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
    Catch ex As System.Data.SqlClient.SqlException
        Dim msg As String = "Insert Error:"
        msg += ex.Message

        Throw New Exception(msg)
    Finally
        conn.Close()
    End Try

3 个答案:

答案 0 :(得分:0)

您永远不会设置Command Text 属性 您根本不需要设置Command Type

答案 1 :(得分:0)

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles

MyBase.Load

dim dt as new datatable

   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblGender )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox1
        .DataSource = dt
        .DisplayMember = "Gender"
    End With


    dim dt1 as new datatable

   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblStatus )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox2
        .DataSource = dt1
        .DisplayMember = "Status"
    End With


    dim dt2 as new datatable
   constr.Open()
    cmd = New OleDbCommand("SELECT *  FROM tblDepartment )
    da = New OleDbDataAdapter(cmd)
    da.Fill(dt)
    constr.Close()
    With ComboBox3
        .DataSource = dt2
        .DisplayMember = "Department"
    End With

    End Sub

答案 2 :(得分:0)

请参阅

Dim conn As New SqlConnection(GetConnectionString())
Dim sb As New StringBuilder(String.Empty)

Dim splitItems As String() = Nothing
For Each item As String In sc

    'Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
    'If item.Contains(",") Then
    '    splitItems = item.Split(",".ToCharArray())
    '    sb.AppendFormat("{0}('{1}'); ", sqlStatement, splitItems(0))

    'End If

    Const sqlStatement As String = "INSERT INTO Date (dateID,date) VALUES"
    If item.Contains(",") Then
        splitItems = item.Split(",".ToCharArray())
        sb.AppendFormat("{0}({1},'{2}'); ", sqlStatement, splitItems(0), splitItems(1))

    End If

Next

Try
    conn.Open()
    Dim cmd As New SqlCommand(sb.ToString(), conn)

    cmd.CommandType = CommandType.Text
    cmd.ExecuteNonQuery()


    Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
Catch ex As System.Data.SqlClient.SqlException
    Dim msg As String = "Insert Error:"
    msg += ex.Message

    Throw New Exception(msg)
Finally
    conn.Close()
End Try