none
請問如何將table值逐筆插入資料庫特定欄位中 使用visual studio 2010 RRS feed

  • 問題

  • 請問個為大大 如何將table值逐筆插入資料庫中

    我要將table3 的資料逐筆插入資料庫中 程式碼如下

    但出現錯誤  謝謝指教

     Using SQLConnection As New MySqlConnection(connectionString)
                Using sqlCommand As New MySqlCommand()
                    For a As Integer = 0 To table3.Rows.Count - 1
                        With sqlCommand
                            .CommandText = "INSERT INTO vtiger_account (accountid, account_no, accountname, ownership, siccode, phone, fax) values (@accountid,@account_no,@accountname,@ownership,@siccode,@phone,@fax)"
                            .Connection = SQLConnection
                            .CommandType = CommandType.Text
                            .Parameters.AddWithValue("@accountid", SqlDbType.Int).Value = table3.Rows.Item(0)
                            .Parameters.AddWithValue("@account_no", SqlDbType.NVarChar).Value = table3.Rows.Item(1)
                            .Parameters.AddWithValue("@accountname", SqlDbType.NVarChar).Value = table3.Rows.Item(2)
                            .Parameters.AddWithValue("@ownership", SqlDbType.NVarChar).Value = table3.Rows.Item(3)
                            .Parameters.AddWithValue("@siccode", SqlDbType.NVarChar).Value = table3.Rows.Item(4)
                            .Parameters.AddWithValue("@phone", SqlDbType.NVarChar).Value = table3.Rows.Item(5)
                            .Parameters.AddWithValue("@fax", SqlDbType.NVarChar).Value = table3.Rows.Item(6)
                        End With
                    Next
                    Try
                        SQLConnection.Open()
                        sqlCommand.ExecuteNonQuery()
                        iReturn = True
                    Catch ex As MySqlException
                        MsgBox(ex.Message.ToString)
                        iReturn = False
                    Finally
                        SQLConnection.Close()
                    End Try
                End Using
            End Using

    2019年7月1日 上午 09:16