Solved

How do I export a mysql query results to excel sheet?

Posted on 2008-06-09
2
2,106 Views
Last Modified: 2013-11-27
Hi,

How do I export a mysql query result to an Excel sheet in Vb.net.This is my code until now:
Imports MySql.Data.MySqlClient
Imports System.Data
 
Public Class FrmMain
    Dim conn As MySqlConnection
    Dim myCommand As New MySqlCommand()
    Dim myAdapter As New MySqlDataAdapter()
    Dim myData As New DataTable()
    Dim SQL As String
 
    Private myConnString As String
 
    Public WriteOnly Property connectionString() As String
        Set(ByVal value As String)
            myConnString = value
            conn = New MySqlConnection()
            conn.ConnectionString = myConnString
 
            Try
                conn.Open()
            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database: " & myerror.Message)
            End Try
        End Set
    End Property
 
    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        Dim conn As New MySqlConnection()
        Dim myCommand As New MySqlCommand()
        Dim myAdapter As New MySqlDataAdapter()
        Dim myData As New DataTable()
        Dim SQL As String
        Dim Hash5 As XmlWriteMode
 
 
        SQL = "SELECT filereputationsummary.fileMD5 FROM filereputationsummary" _
                & " WHERE `numSubmissions`>100000" _
                & " Order by `numSubmissions`;"
        'conn = New MySqlConnection()
        conn.ConnectionString = myConnString
 
        Try
            conn.Open()
 
            Try
                myCommand.Connection = conn
                myCommand.CommandText = SQL
 
                myAdapter.SelectCommand = myCommand
                myAdapter.Fill(myData)
 
 
                DGVReport.DataSource = myData
                DGVReport.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
                'myData.WriteXml(Hash5)
 
                Me.Show()
 
            Catch myerror As MySqlException
                MsgBox("There was an error reading from the database: " & myerror.Message)
            End Try
        Catch myerror As MySqlException
            MessageBox.Show("Error connecting to the database: " & myerror.Message)
        Finally
            If conn.State <> ConnectionState.Closed Then conn.Close()
        End Try
    End Sub
 
End Class

Open in new window

0
Comment
Question by:joshua_123_99
2 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 125 total points
ID: 21748907
Exporting dataset to excel file.
http://www.dotnetspider.com/resources/693-Exporting-dataset-excel-file.aspx

Export to Excel using VB.Net
http://www.codeproject.com/KB/vb/Senthil_S__Software_Eng_.aspx

HOW TO: Transfer Data to Excel Workbook Using Visual Basic .NET
http://support.microsoft.com/?scid=kb;en-us;Q306022

Export data table to Excel
http://www.vbcode.com/Asp/showsn.asp?theID=12697
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Lookup - Vlookup 9 42
Formula returning #N/A 9 31
What is wrong with this Excel formula with an IF statement? 6 31
Need Excel Formula for extract out date only 17 38
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question