Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Export SQL Server Data to Excel Using C#?

Export SQL Server Data to Excel Using C#?

Scheduled Pinned Locked Moved C#
csharpdatabasemysqlsql-serversysadmin
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OldZero
    wrote on last edited by
    #1

    I'm working on an assignment that requires a simple data transfer data of a SQL Server to MS Excel; using C#. I was able to develop the "foundation" of what I believe works/ However I'm unable to run the program. Assistance is appreciated!

    namespace ProjectLab1
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Connection string needed to talk to Omnymbus
    string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
    + "Server=devry.edupe.net;Port=4300;"
    + "Database=Inventory_XXXX;"
    + "uid=XXXX;pwd=DeVry_Student";
    OdbcConnection connection = new OdbcConnection(conString);
    connection.Open();
    //Get all data
    //string theQuery = "SELECT * FROM item";
    string theQuery = "SELECT * FROM item i, inventory v where i.invent_id=v.invent_id";
    OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
    DataSet theData = new DataSet();
    DataAdapter.Fill(theData, "item");
    DataTable theTable = theData.Tables[0];

            Console.Write("ITEM".PadRight(30));
            Console.Write("QUANT".PadLeft(10));
            Console.Write("SIZE".PadLeft(10));
            Console.Write("COLOR".PadLeft(10));
            Console.Write("PRICE\\n".PadLeft(10));//the "\\n" moves cursor to next line
            Console.WriteLine("-".PadLeft(70, '-'));// "WriteLine" places cursor next line
    
            // Loop through all data results
            foreach (DataRow dataRow in theTable.Rows)
            {
                Console.Write(dataRow\["invent\_desc"\].ToString().PadRight(30));
                Console.Write(dataRow\["qoh"\].ToString().PadLeft(10));
                Console.Write(dataRow\["itemsize"\].ToString().PadLeft(10));
                Console.Write(dataRow\["color"\].ToString().PadLeft(10));
                Console.WriteLine(((decimal)dataRow\["curr\_price"\]).ToString("C").PadLeft(10));
    
            }
    
            {
    
    
    
                StreamWriter file = new StreamWriter(@"C:\\Users\\redfaction\\Desktop\\Data\\" + strFileName);
                file.WriteLine(sb.ToString());
                File.Close();
                connection.Close(); // Close connection
    
                //Have program pause to keep from closing console window
    
    
    
            }
        }
    }
    

    }

    Richard Andrew x64R L 3 Replies Last reply
    0
    • O OldZero

      I'm working on an assignment that requires a simple data transfer data of a SQL Server to MS Excel; using C#. I was able to develop the "foundation" of what I believe works/ However I'm unable to run the program. Assistance is appreciated!

      namespace ProjectLab1
      {
      class Program
      {
      static void Main(string[] args)
      {
      //Connection string needed to talk to Omnymbus
      string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
      + "Server=devry.edupe.net;Port=4300;"
      + "Database=Inventory_XXXX;"
      + "uid=XXXX;pwd=DeVry_Student";
      OdbcConnection connection = new OdbcConnection(conString);
      connection.Open();
      //Get all data
      //string theQuery = "SELECT * FROM item";
      string theQuery = "SELECT * FROM item i, inventory v where i.invent_id=v.invent_id";
      OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
      DataSet theData = new DataSet();
      DataAdapter.Fill(theData, "item");
      DataTable theTable = theData.Tables[0];

              Console.Write("ITEM".PadRight(30));
              Console.Write("QUANT".PadLeft(10));
              Console.Write("SIZE".PadLeft(10));
              Console.Write("COLOR".PadLeft(10));
              Console.Write("PRICE\\n".PadLeft(10));//the "\\n" moves cursor to next line
              Console.WriteLine("-".PadLeft(70, '-'));// "WriteLine" places cursor next line
      
              // Loop through all data results
              foreach (DataRow dataRow in theTable.Rows)
              {
                  Console.Write(dataRow\["invent\_desc"\].ToString().PadRight(30));
                  Console.Write(dataRow\["qoh"\].ToString().PadLeft(10));
                  Console.Write(dataRow\["itemsize"\].ToString().PadLeft(10));
                  Console.Write(dataRow\["color"\].ToString().PadLeft(10));
                  Console.WriteLine(((decimal)dataRow\["curr\_price"\]).ToString("C").PadLeft(10));
      
              }
      
              {
      
      
      
                  StreamWriter file = new StreamWriter(@"C:\\Users\\redfaction\\Desktop\\Data\\" + strFileName);
                  file.WriteLine(sb.ToString());
                  File.Close();
                  connection.Close(); // Close connection
      
                  //Have program pause to keep from closing console window
      
      
      
              }
          }
      }
      

      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Member 11527519 wrote:

      I'm unable to run the program.

      Care to be more specific? How is anyone supposed to know what you mean by that?

      The difficult we do right away... ...the impossible takes slightly longer.

      O 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        Member 11527519 wrote:

        I'm unable to run the program.

        Care to be more specific? How is anyone supposed to know what you mean by that?

        The difficult we do right away... ...the impossible takes slightly longer.

        O Offline
        O Offline
        OldZero
        wrote on last edited by
        #3

        I'm sorry about that. The main issue here is that the code simply doesn't work and I've tried different ways to make a cmd-based program take a .sql file and export the data as a MS Excel file - as end result. Out of all the attempts I've made; this is the closest I've gotten but I'm unable to run the program due to a small number of errors I'm not familiar with. If possible I would appreciate it if you took a look at the code though a program like Visual Studio.

        S P 2 Replies Last reply
        0
        • O OldZero

          I'm working on an assignment that requires a simple data transfer data of a SQL Server to MS Excel; using C#. I was able to develop the "foundation" of what I believe works/ However I'm unable to run the program. Assistance is appreciated!

          namespace ProjectLab1
          {
          class Program
          {
          static void Main(string[] args)
          {
          //Connection string needed to talk to Omnymbus
          string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
          + "Server=devry.edupe.net;Port=4300;"
          + "Database=Inventory_XXXX;"
          + "uid=XXXX;pwd=DeVry_Student";
          OdbcConnection connection = new OdbcConnection(conString);
          connection.Open();
          //Get all data
          //string theQuery = "SELECT * FROM item";
          string theQuery = "SELECT * FROM item i, inventory v where i.invent_id=v.invent_id";
          OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
          DataSet theData = new DataSet();
          DataAdapter.Fill(theData, "item");
          DataTable theTable = theData.Tables[0];

                  Console.Write("ITEM".PadRight(30));
                  Console.Write("QUANT".PadLeft(10));
                  Console.Write("SIZE".PadLeft(10));
                  Console.Write("COLOR".PadLeft(10));
                  Console.Write("PRICE\\n".PadLeft(10));//the "\\n" moves cursor to next line
                  Console.WriteLine("-".PadLeft(70, '-'));// "WriteLine" places cursor next line
          
                  // Loop through all data results
                  foreach (DataRow dataRow in theTable.Rows)
                  {
                      Console.Write(dataRow\["invent\_desc"\].ToString().PadRight(30));
                      Console.Write(dataRow\["qoh"\].ToString().PadLeft(10));
                      Console.Write(dataRow\["itemsize"\].ToString().PadLeft(10));
                      Console.Write(dataRow\["color"\].ToString().PadLeft(10));
                      Console.WriteLine(((decimal)dataRow\["curr\_price"\]).ToString("C").PadLeft(10));
          
                  }
          
                  {
          
          
          
                      StreamWriter file = new StreamWriter(@"C:\\Users\\redfaction\\Desktop\\Data\\" + strFileName);
                      file.WriteLine(sb.ToString());
                      File.Close();
                      connection.Close(); // Close connection
          
                      //Have program pause to keep from closing console window
          
          
          
                  }
              }
          }
          

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          "Unable to run"? There are so many things wrong with this code, I don't think it even compiles. The best part is where you create a flat file (with a name ending in ".xls") and somehow think this is "Excel"? You're doomed.

          1 Reply Last reply
          0
          • O OldZero

            I'm working on an assignment that requires a simple data transfer data of a SQL Server to MS Excel; using C#. I was able to develop the "foundation" of what I believe works/ However I'm unable to run the program. Assistance is appreciated!

            namespace ProjectLab1
            {
            class Program
            {
            static void Main(string[] args)
            {
            //Connection string needed to talk to Omnymbus
            string conString = "Driver={MySQL ODBC 5.3 ANSI Driver};"
            + "Server=devry.edupe.net;Port=4300;"
            + "Database=Inventory_XXXX;"
            + "uid=XXXX;pwd=DeVry_Student";
            OdbcConnection connection = new OdbcConnection(conString);
            connection.Open();
            //Get all data
            //string theQuery = "SELECT * FROM item";
            string theQuery = "SELECT * FROM item i, inventory v where i.invent_id=v.invent_id";
            OdbcDataAdapter DataAdapter = new OdbcDataAdapter(theQuery, connection);
            DataSet theData = new DataSet();
            DataAdapter.Fill(theData, "item");
            DataTable theTable = theData.Tables[0];

                    Console.Write("ITEM".PadRight(30));
                    Console.Write("QUANT".PadLeft(10));
                    Console.Write("SIZE".PadLeft(10));
                    Console.Write("COLOR".PadLeft(10));
                    Console.Write("PRICE\\n".PadLeft(10));//the "\\n" moves cursor to next line
                    Console.WriteLine("-".PadLeft(70, '-'));// "WriteLine" places cursor next line
            
                    // Loop through all data results
                    foreach (DataRow dataRow in theTable.Rows)
                    {
                        Console.Write(dataRow\["invent\_desc"\].ToString().PadRight(30));
                        Console.Write(dataRow\["qoh"\].ToString().PadLeft(10));
                        Console.Write(dataRow\["itemsize"\].ToString().PadLeft(10));
                        Console.Write(dataRow\["color"\].ToString().PadLeft(10));
                        Console.WriteLine(((decimal)dataRow\["curr\_price"\]).ToString("C").PadLeft(10));
            
                    }
            
                    {
            
            
            
                        StreamWriter file = new StreamWriter(@"C:\\Users\\redfaction\\Desktop\\Data\\" + strFileName);
                        file.WriteLine(sb.ToString());
                        File.Close();
                        connection.Close(); // Close connection
            
                        //Have program pause to keep from closing console window
            
            
            
                    }
                }
            }
            

            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Start over by first studying and running this example: Using MySQL With C#[^] Oh ... And why are you talking "SQL Server" even though you are using a MySQL ODBC driver?

            1 Reply Last reply
            0
            • O OldZero

              I'm sorry about that. The main issue here is that the code simply doesn't work and I've tried different ways to make a cmd-based program take a .sql file and export the data as a MS Excel file - as end result. Out of all the attempts I've made; this is the closest I've gotten but I'm unable to run the program due to a small number of errors I'm not familiar with. If possible I would appreciate it if you took a look at the code though a program like Visual Studio.

              S Offline
              S Offline
              Subramanyam Shankar
              wrote on last edited by
              #6

              Have you tried the writing to excel using the interop and you are connecting using MySQL odbc. is your db really sql server?

              O 1 Reply Last reply
              0
              • S Subramanyam Shankar

                Have you tried the writing to excel using the interop and you are connecting using MySQL odbc. is your db really sql server?

                O Offline
                O Offline
                OldZero
                wrote on last edited by
                #7

                Yes, it is a server that is holding SQL Datatables on a cloudserver that can be connected using ODBC. I'm able to connect to this server (that I have done before using cmd-based program to simply see particular tables, which HAS worked) I'm just simply trying to figure out how to export the data to MS Excel, by making a CSV file.

                1 Reply Last reply
                0
                • O OldZero

                  I'm sorry about that. The main issue here is that the code simply doesn't work and I've tried different ways to make a cmd-based program take a .sql file and export the data as a MS Excel file - as end result. Out of all the attempts I've made; this is the closest I've gotten but I'm unable to run the program due to a small number of errors I'm not familiar with. If possible I would appreciate it if you took a look at the code though a program like Visual Studio.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  What are these small number of errors? Are they compilation errors? Are they runtime exceptions?

                  O 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    What are these small number of errors? Are they compilation errors? Are they runtime exceptions?

                    O Offline
                    O Offline
                    OldZero
                    wrote on last edited by
                    #9

                    There are about 6 errors in total; But 5 are name errors giving the message of "ddlExportFormat, strFormat, ds and connection does not exist in current content". I felt like within the code I posted above they where accurately addressed towards their methods. and the Last error address a System.IO file saying

                    'System.IO.File; does not contain a definition for Close"

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups