0

this code is in the button click , i get each data out using spilt but i encounter error at "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

the same code , the below work 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
4

3 に答える 3

0

Command Textプロパティを設定することはありません。Command Type
を設定する必要はまったくありません。

于 2012-01-31T18:08:01.677 に答える