Importing Excel files with a large number of columns header into mysql with c#

风格不统一 提交于 2019-12-11 01:45:22

问题


i just was just wondering, how do i import large excel files into mysql with c#? My coding experience isn't great and i was hoping if there's anyone out there who could give me some rough idea to start on it. So far, i was able to load excel files into datagridview with the following codes:

string PathConn = " Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + pathTextBox.Text + ";Extended Properties =\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(PathConn);
conn.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + loadTextBox.Text + "$]", conn);
table = new DataTable();             

myDataAdapter.Fill(table);    

but after that, i don't know how i could extract the information and save it into mysql database. Assuming i have a empty scheme created before, how do i work on uploading excel files into mysql? thanks.


回答1:


I think you would then need to loop over the items in the datatable and do something with them (maybe an insert statement to your DB)

like so

foreach(DataRow dr in table.Rows)
{
    string s = dr[0].ToString() // this will be the first column in the datatabl as they are zero indexed
}



回答2:


this is what i do in data migration scenarios from one SQL Server to another or DataFiles to SQL:

  1. Create the new Table on the destination SQL Server (Column names, Primary Key etc.)
  2. Load existing Data into a DataTable (Thats what you did already)
  3. Now Query the new Table with the DataAdapter into another DataTable (Same as you did with the excel file except you now query the SQL Table.)
  4. Load OldData from 'table' into 'newTable' using DataTable Method "Load()"
string PathConn = (MYSQL Connection String goes here)
OleDbConnection conn = new OleDbConnection(PathConn);
conn.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + loadTextBox.Text + "$]", conn);
newTable = new DataTable();             

myDataAdapter.Fill(newTable); 

Now use the Load() Method on the new table:

newTable.Load(table.CreateDataReader(), <Specify LoadOption here>)

Matching columns will be imported into the new DataTable. (You can ensure the mapping through using Aliases in the select statements)

After Loading the existing Data into the new Table you will be able to use an DataAdapter to write the changes back to database.

Example for writing data back: ConnString - connection String for DB, SelectStmt (can use the same as you did on the empty Table before) and provide the newTable as dtToWrite

public static void writeDataTableToServer(string ConnString, string selectStmt, DataTable dtToWrite)
    {
        using (OdbcConnection odbcConn = new OdbcConnection(ConnString))
        {
            odbcConn.Open();

            using (OdbcTransaction trans = odbcConn.BeginTransaction())
            {
                using (OdbcDataAdapter daTmp = new OdbcDataAdapter(selectStmt, ConnString))
                {
                    using (OdbcCommandBuilder cb = new OdbcCommandBuilder(daTmp))
                    {

                        try
                        {
                            cb.ConflictOption = ConflictOption.OverwriteChanges;
                            daTmp.UpdateBatchSize = 5000;
                            daTmp.SelectCommand.Transaction = trans;
                            daTmp.SelectCommand.CommandTimeout = 120;
                            daTmp.InsertCommand = cb.GetInsertCommand();
                            daTmp.InsertCommand.Transaction = trans;
                            daTmp.InsertCommand.CommandTimeout = 120;
                            daTmp.UpdateCommand = cb.GetUpdateCommand();
                            daTmp.UpdateCommand.Transaction = trans;
                            daTmp.UpdateCommand.CommandTimeout = 120;
                            daTmp.DeleteCommand = cb.GetDeleteCommand();
                            daTmp.DeleteCommand.Transaction = trans;
                            daTmp.DeleteCommand.CommandTimeout = 120;
                            daTmp.Update(dtToWrite);
                            trans.Commit();
                        }
                        catch (OdbcException ex)
                        {
                            trans.Rollback();
                            throw ex;
                        }

                    }
                }
            }
            odbcConn.Close();
        }
    }

Hope this helps.

Primary Key on the newTable is necessary, otherwise you might get a CommandBuilder exception.

BR

Therak




回答3:


Your halfway there, You have obtained the information from the Excel spreadsheet and have it stored in a DataTable.

The first thing you need to do before you look to import a significant amount of data into SQL is validate what you have read in from the spreadsheets.

You have a few options, one of which is do something very similar to how you read in your data and that is use a SQLAdapter to perform am INSERT into the SQL Database. All your really needing to do in this case is create a new connection and write the INSERT command.

There are many example of doing this on here.

Another option which i would use, is LINQ to CSV (http://linqtocsv.codeplex.com/).

With this you can load all of your data into class objects which makes it easier to validate each object before you perform your INSERT into SQL.

If you have limited experience then use the SQLAdapter to connect to you DB.

Good Luck



来源:https://stackoverflow.com/questions/19133317/importing-excel-files-with-a-large-number-of-columns-header-into-mysql-with-c-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!