Solved

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

Posted on 2008-06-09
2
2,052 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

862 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now