0

此代码在按钮单击中,我使用溢出获取每个数据,但在“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
4

3 回答 3

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
于 2013-12-20T06:36:31.717 回答
0

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

MyBase.Load

暗淡 dt 作为新的数据表

   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
于 2013-06-18T09:39:39.697 回答
0

您永远不会设置命令文本属性。
您根本不需要设置命令类型

于 2012-01-31T18:08:01.677 回答