Export table data in Access Database

Hi,

I need to export a table data in Access Database into a text file using C# interface by a press of a button. I need advise on which method is required to use for this Tks.



Answer this question

Export table data in Access Database

  • BobConsultant

    You should indicate the namespace, for example, to use the StreamWriter and server class, you should add

    using System.IO;

    using Microsoft.SqlServer.Management.Smo;



  • stupots

    The declaration and error as follow:

    using System.IO;

    using Microsoft.SqlServer.Management.Smo;

    Error:

    The type or namespace name 'SqlServer' does not exist in the class or namespace 'Microsoft' (are you missing an assembly reference )


  • Jithendrian

    I don't think so. What error messages did you get

  • Barry Kwok

    Hope following codes are helpful for you. But I haven't compiled and test them, just for your reference.

    //Pass the filepath and filename to the StreamWriter Constructor
    StreamWriter sw = new StreamWriter("C:\\Test.txt");
    string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database/TeamPro.mdb");  
    string strSQL = "SELECT * FROM Project where id=" + Convert.ToInt32(Request.QueryString["Project_ID"]) ;

    // create Objects of ADOConnection and ADOCommand
    OleDbConnection myConn = new OleDbConnection(strDSN);
    OleDbCommand myCmd = new OleDbCommand( strSQL, myConn );
    OleDbDataReader datareader = null;
    try
    {
    myConn.Open();
    datareader = myCmd.ExecuteReader();
    while (datareader.Read() )
    {
    //Write a line of text
    sw.WriteLine("datareader["Project_Name"].ToString() + " "+datareader["Project_Manager"].ToString()+" "+datareader["Project_Status"].ToString());
    }
    }
    catch (Exception e)
    {
    string Messate = e.Message;
    }
    finally
    {
    myConn.Close();
    }



  • KerryLW

    I tested on my Login Table but received the following error:

    private void btnAR_Click(object sender, System.EventArgs e)

    {

    //Pass the filepath and filename to the StreamWriter Constructor

    StreamWriter sw = new StreamWriter("C:\\JSM.txt");

    string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/Database.mdb");

    string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ;

    // create Objects of ADOConnection and ADOCommand

    OleDbConnection myConn = new OleDbConnection(strDSN);

    OleDbCommand myCmd = new OleDbCommand( strSQL, myConn );

    OleDbDataReader datareader = null;

    try

    {

    myConn.Open();

    datareader = myCmd.ExecuteReader();

    while (datareader.Read() )

    {

    //Write a line of text

    sw.WriteLine("datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());

    }

    }

    catch (Exception e)

    {

    string Messate = e.Message;

    }

    finally

    {

    myConn.Close();

    }

    }

    ) expected

    ; expected

    ; expected

    ; expected

    ; expected

    ; expected

    Newline in constant


  • danleis

    Sorry, it should be

    sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());

    }

    No the first quotation mark before the "datareader"



  • idos

    You need to add "using System.IO;" so that the StreamWriter type can be resolved. Server and Request are properties of the ASP.NET page class. If you are creating a Windows Forms application you will not have access to them. You don't need MapPath at all; just use the path you have. And where is the User_ID coming from

  • Ron DeSerranno - MOBIFORM

    I have tried and compile without error, but it does not seems to be running. I put an Invoke message to test but when I click the button, nothing happens.

    -------------------

    private void btnAR_Click(object sender, System.EventArgs e)

    {

    //Pass the filepath and filename to the StreamWriter Constructor

    StreamWriter sw = new StreamWriter("C:/JSM.txt");

    string database = "C:/M8099_Project/Database/Invoicing_Database.mdb";

    string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + database;

    string strSQL = "SELECT * FROM Login WHERE User_ID = 1";

    // create Objects of ADOConnection and ADOCommand

    OleDbConnection myConn = new OleDbConnection(strConn);

    OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);

    OleDbDataReader datareader = null;

    try

    {

    myConn.Open();

    datareader = myCmd.ExecuteReader();

    while (datareader.Read() )

    {

    //Write a line of text

    sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());

    string message = "Export complete!";

    string caption = "Export Database!";

    MessageBoxButtons buttons = MessageBoxButtons.OK;

    MessageBoxIcon icon = MessageBoxIcon.Warning;

    DialogResult result;

    // Displays the MessageBox

    result = MessageBox.Show(this, message, caption, buttons, icon);

    }

    }

    catch //(Exception e)

    {

    string message = "Export error!";

    string caption = "Export Warning!";

    MessageBoxButtons buttons = MessageBoxButtons.OK;

    MessageBoxIcon icon = MessageBoxIcon.Warning;

    DialogResult result;

    // Displays the MessageBox

    result = MessageBox.Show(this, message, caption, buttons, icon);

    }

    finally

    {

    myConn.Close();

    }

    }


  • pdns

    Hi, i am not using an SQLserver, so there is the namespace error again. So, meaning I have to install an SQL Server
  • BobLoblaw

    In winform application, connection string should be something like:

    string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= D:\App_Data\Northwind.mdb");

    query statement should be something like:

    string strSQL = "SELECT * FROM Login where id=" + User_ID ;//User_ID should be a string

    please modify the code above according to your reqirement



  • Quinn01

    This is a windows application.
  • Rhubarb

    Some missing directive or assembly reference error again.  Is it because of the SQL

     

    private void btnAR_Click(object sender, System.EventArgs e)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    {

      //Pass the filepath and filename to the StreamWriter Constructor

      StreamWriter sw = new StreamWriter("C:\\JSM.txt");

      string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/M8099_Project/Database/Invoicing_Database.mdb"); 

      string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ; 

     

      // create Objects of ADOConnection and ADOCommand 

      OleDbConnection myConn = new OleDbConnection(strDSN); 

      OleDbCommand myCmd = new OleDbCommand(strSQL, myConn); 

      OleDbDataReader datareader = null; 

                   

      try 

      { 

        myConn.Open(); 

        datareader = myCmd.ExecuteReader(); 

        while (datareader.Read() ) 

        { 

          //Write a line of text

          sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());

        } 

      } 

      catch 

      { 

        string message = "Export error!";

        string caption = "Export Warning!";

        MessageBoxButtons buttons = MessageBoxButtons.OK;

        MessageBoxIcon icon = MessageBoxIcon.Warning;

        DialogResult result;

     

        // Displays the MessageBox

        result = MessageBox.Show(this, message, caption, buttons, icon);

      } 

      finally 

      { 

        myConn.Close(); 

      }

    }

     

    The type or namespace name 'StreamWriter' could not be found (are you missing a using directive or an assembly reference )
    The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference )
    The type or namespace name 'Request' could not be found (are you missing a using directive or an assembly reference )
    The type or namespace name 'sw' could not be found (are you missing a using directive or an assembly reference )


  • prthealien

    Did you read my last post

    CommonGenius.com wrote:

    Server and Request are properties of the ASP.NET page class. If you are creating a Windows Forms application you will not have access to them.

    Is this a windows or web application



  • Meir P

    This is the error I get if I quote the //using Microsoft.SqlServer.Management.Smo;

    I have a MS Access Database with a Table name "Login" and Column 1 "User_ID" and "Password". The User_ID and Password is the actual column name I used in the MS Access.

    --------------

    StreamWriter sw = new StreamWriter("C:\\JSM.txt");

    string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("C:/M8099_Project/Database/Invoicing_Database.mdb");

    string strSQL = "SELECT * FROM Login where id=" + Convert.ToInt32(Request.QueryString["User_ID"]) ;

    // create Objects of ADOConnection and ADOCommand

    OleDbConnection myConn = new OleDbConnection(strDSN);

    OleDbCommand myCmd = new OleDbCommand(strSQL, myConn);

    OleDbDataReader datareader = null;

    try

    {

    myConn.Open();

    datareader = myCmd.ExecuteReader();

    while (datareader.Read() )

    {

    //Write a line of text

    sw.WriteLine(datareader["User_ID"].ToString()+" "+datareader["Password"].ToString());

    }

    }

    catch

    {

    string message = "Export error!";

    string caption = "Export Warning!";

    MessageBoxButtons buttons = MessageBoxButtons.OK;

    MessageBoxIcon icon = MessageBoxIcon.Warning;

    DialogResult result;

    // Displays the MessageBox

    result = MessageBox.Show(this, message, caption, buttons, icon);

    }

    finally

    {

    myConn.Close();

    }

    Error:

    The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference )
    The type or namespace name 'Request' could not be found (are you missing a using directive or an assembly reference )


  • Export table data in Access Database