0

how we can export or import data from or to to Datagridview from Excel or Access file?? I need to know the code for export and import..any one help please

2
  • 3
    You can do google if you need the code. If you need help with a specif issue in your code, ask for help. If you haven't tried anything, I guess it would be difficult for others to help you. Commented Sep 4, 2012 at 5:34
  • 2
    I am surprised how this question has been upvoted Commented Sep 4, 2012 at 5:34

2 Answers 2

1

try this:

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
            MyCommand.TableMappings.Add("Table", "Net-informations.com");
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);
            dataGridView1.DataSource = DtSet.Tables[0];
            MyConnection.Close();
        }
    }
}

Here is another tutorial or just use google

Sign up to request clarification or add additional context in comments.

Comments

0

I'll try to extend previous post with Access sample:

    DataTable LoadSchemaFromAccess(string szFilePath)
    {

    System.Data.OleDb.OleDbCommand cmd;
    try
    {
        System.Data.OleDb.OleDbConnection cnn  = new    System.Data.OleDb.OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data  Source={0};Persist Security Info=False;", szFilePath));
            cnn.Open();
            System.Data.DataTable schemaTable = cnn.GetSchema("Tables");

            cnn.Close();
            return schemaTable;
    }
    catch (exception e)
    {
        MessageBox.Show(e.Message);
        return null;
    }
    finally
    {
        if (cmd != null)
        {
            cmd.Dispose();
        }
    }
}

    string LoadDataFromAccess(string szTableName )
    {
 string   GetData = L"SELECT * FROM " + szTableName;
 System.Data.OleDb.OleDbCommand cmd;
 string szColumns = "";
try
{
    System.Data.OleDb.OleDbConnection cnn = new      System.Data.OleDb.OleDbConnection
        (string.Format(L"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Persist Security Info=False;", szPath));
    cnn.Open();
    System.Data.DataTable dt = new System.Data.DataTable();
    OleDbCommand  cmd = new OleDbCommand();
    cmd.Connection = cnn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = GetData;
    OleDbDataAdapter adt = new OleDbDataAdapter(cmd);
    adt.SelectCommand = cmd;
    adt.Fill(dt);
     cnn.Close();
     return dt;
}
catch (exception e)
{
    MessageBox.Show(e.Message);
    return null;
}
finally
{
    if (cmd != null)
     {
        cmd.Dispose();
    }
}
}

Hope this helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.