import csv file/excel into sql database asp.net

后端 未结 5 1202
傲寒
傲寒 2020-12-14 05:02

I am starting a project with asp.net visual studio 2008 / SQL 2000 (2005 in future) using c#.

The tricky part for me is that the existing DB schema changes often and

相关标签:
5条回答
  • 2020-12-14 05:16

    FileHelpers is your friend. I've used it happily for several projects and it has saved me much grief and labour

    0 讨论(0)
  • 2020-12-14 05:17

    Check out the excellent FileHelpers library - there an article on CodeProject about it, and it's hosted here.

    It's pure C#, and it can import just about any flat-file, CSV, and it deals with Excel, too. The import is totally configurable - you can define things like delimiters, rows and/or columns to skip, and so on - lots of options.

    I've successfully used it in various projects and it just works flawlessly - highly recommended.

    0 讨论(0)
  • 2020-12-14 05:29

    I am using csvReader from LumenWorks.Framework for uploading and importing the csv files into a sql table and a DataTable in memory which I create based on the columns imported.

    I also have the user map the fields in the ui and proceed to validate and prepare the data for import by labeling each record as insert/update/error. I then create/populate strongly typed DataSet for each table that will be affected and build the insert/update queries for Enterprise Library UpdateDataset() method.

    Then I submit the transaction to insert/update the database. –

    The mapping is a grid with 4 columns. ( import field name, destination table, destination field name, ignore, match status), with destination table and field names being selects which refresh based on table selected. I dynamically populate the selects. Initially select combo is populated with 1 value if the match is found, or please select if it isn't. ignore allows user to ignore the field. match status is if a field was auto-mapped

    0 讨论(0)
  • 2020-12-14 05:30

    I suspect there may exist some robust and flexible tools to help you with this potentially very complex application that hopefully someone will point you to.

    In the mean time, here is a function I have found useful to covert an excel file to a DataTable. This version only looks for the first worksheet. It might serve as a starting point. To to something similar with csv files should only require modifying the connection string.

    public static DataTable GetDataTableFromExcel(string SourceFilePath)
    {
        string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                    "Data Source=" + SourceFilePath + ";" +
                                    "Extended Properties=Excel 8.0;";
    
        using (OleDbConnection cn = new OleDbConnection(ConnectionString))
        {
            cn.Open();
    
            DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (dbSchema == null || dbSchema.Rows.Count < 1)
            {
                throw new Exception("Error: Could not determine the name of the first worksheet.");
            }
    
            string WorkSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
    
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + WorkSheetName + "]", cn);
            DataTable dt = new DataTable(WorkSheetName);
    
            da.Fill(dt);
    
            return dt;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 05:33

    You can easily create an IDataReader over an Excel or CSV file (see http://support.microsoft.com/kb/316934).

    Are you using SQL Server as your database engine? If so, you can use the SqlBulkCopy class (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx) to efficiently take your IDataReader, map columns as appropriate, and store the results in your database.

    0 讨论(0)
提交回复
热议问题