从CSV文件将数据导出到SQL Server 2005

时间:2013-10-15 11:35:26

标签: asp.net sql-server-2005

asp.net 代码中从CSV文件中提取数据并将其插入SQL Server 2005表。

但是,我需要在代码中静态指定表名,将提取其数据的字段名,以及映射信息(“CSV的列名”和“表的列名”之间的映射)。

如何修改以下代码,使其适用于任何表名 简而言之,表名和映射应该动态

public class CsvBulkCopyDataIntoSqlServer
{

    protected const string _truncateLiveTableCommandText = @"TRUNCATE TABLE Account";
    protected const int _batchSize = 100000;

    static void Main(string[] args)
    {
        LoadCsvDataIntoSqlServer();

    }

    public static void LoadCsvDataIntoSqlServer()
    {
        // This should be the full path
        var fileName = @"D:\output.csv";
        var createdCount = 0;

        using (var textFieldParser = new TextFieldParser(fileName))
        {
            textFieldParser.TextFieldType = FieldType.Delimited;
            textFieldParser.Delimiters = new[] { "," };
            textFieldParser.HasFieldsEnclosedInQuotes = true;

            // var connectionString = ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString;
            string connectionString = "Data Source= 172.25.10.4" + ";Initial Catalog= SFdata" + ";Persist Security Info=True;User ID= sa" + ";Password= Newuser@123";


            var dataTable = new DataTable("Account");


            // Add the columns in the temp table
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("shippingstreet");
            dataTable.Columns.Add("shippingpostalcode");
            dataTable.Columns.Add("ShippingCountry");
            dataTable.Columns.Add("shippingstate");

            //dataTable.Columns.Add("LastName");

            using (var sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                // Truncate the live table
                using (var sqlCommand = new SqlCommand(_truncateLiveTableCommandText, sqlConnection))
                {
                    sqlCommand.ExecuteNonQuery();
                }

                // Create the bulk copy object
                var sqlBulkCopy = new SqlBulkCopy(sqlConnection)
                {
                    DestinationTableName = "Account"
                };

                // Setup the column mappings, anything ommitted is skipped
                sqlBulkCopy.ColumnMappings.Add("Name", "Name");
                sqlBulkCopy.ColumnMappings.Add("shippingstreet", "shippingstreet");
                sqlBulkCopy.ColumnMappings.Add("shippingpostalcode", "shippingpostalcode");
                sqlBulkCopy.ColumnMappings.Add("ShippingCountry", "ShippingCountry");
                sqlBulkCopy.ColumnMappings.Add("shippingstate", "shippingstate");

                // Loop through the CSV and load each set of 100,000 records into a DataTable
                // Then send it to the LiveTable
                while (!textFieldParser.EndOfData)
                {
                    dataTable.Rows.Add(textFieldParser.ReadFields());

                    createdCount++;

                    if (createdCount % _batchSize == 0)
                    {
                        InsertDataTable(sqlBulkCopy, sqlConnection, dataTable);

                        break;
                    }
                }

                InsertDataTable(sqlBulkCopy, sqlConnection, dataTable);

                sqlConnection.Close();
            }
        }
    }

    protected static void InsertDataTable(SqlBulkCopy sqlBulkCopy, SqlConnection sqlConnection, DataTable dataTable)
    {
        sqlBulkCopy.WriteToServer(dataTable);

        dataTable.Rows.Clear();
    }

}

1 个答案:

答案 0 :(得分:0)

为什么不使用SQL Server Integration Services,它会让您的生活更轻松