简体   繁体   English

如何使用 C# 将 CSV 文件上传到 sql 服务器表?

[英]How to Upload CSV file to sql server table using C#?

I got the error message below when I tried to upload a csv file to a SQL Server table using C# (csv file has no header).当我尝试使用 C#(csv 文件没有标题)将 csv 文件上传到 SQL 服务器表时,我收到以下错误消息。 Error Message: "A column named ' ' already belongs to this DataTable"错误消息:“名为 ' ' 的列已属于此数据表”

I tried to find some solutions somewhere on the web but I'm really stuck with it.我试图在 web 的某处找到一些解决方案,但我真的坚持了下来。 My code:我的代码:

SqlConnection con = new SqlConnection(@"server=.;Initial Catalog=myDtabase;Integrated Security=SSPI;");

            string filepath = @"c:\\my_CSV_file.csv";

            StreamReader sr = new StreamReader(filepath);

            string line = sr.ReadLine();
            string[] value = line.Split(',');
            DataTable dt = new DataTable();
            DataRow row;

            foreach (string dc in value)
            {
                dt.Columns.Add(new DataColumn(dc));
            }

            while (!sr.EndOfStream)
            {
                value = sr.ReadLine().Split(',');
                if (value.Length == dt.Columns.Count)
                {
                    row = dt.NewRow();
                    row.ItemArray = value;
                    dt.Rows.Add(row);
                }
            }

            SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
            bc.DestinationTableName = "my_SQLServer_Table";
            bc.BatchSize = dt.Rows.Count;
            con.Open();
            bc.WriteToServer(dt);
            bc.Close();
            con.Close();

I think this link will help you get this done.我认为此链接将帮助您完成这项工作。

http://forums.asp.net/t/1695615.aspx http://forums.asp.net/t/1695615.aspx

As usual, there is more than one way to skin a cat.像往常一样,给猫剥皮的方法不止一种。 So, if yo don't like the solution listed above, try this script, which I know will work for you.所以,如果你不喜欢上面列出的解决方案,试试这个脚本,我知道它对你有用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;

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

        private void button1_Click(object sender, EventArgs e)
        {

            string server = "EXCEL-PC\\EXCELDEVELOPER";
            string database = "AdventureWorksLT2012";
            string SQLServerConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", server, database);


            string CSVpath = @"C:\Users\Ryan\Documents\Visual Studio 2010\Projects\Bulk Copy from CSV to SQL Server Table\WindowsFormsApplication1\bin"; // CSV file Path
            string CSVFileConnectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};;Extended Properties=\"text;HDR=Yes;FMT=Delimited\";", CSVpath);

            var AllFiles = new DirectoryInfo(CSVpath).GetFiles("*.CSV");
            string File_Name = string.Empty;

            foreach (var file in AllFiles)
            {
                try
                {
                    DataTable dt = new DataTable();
                    using (OleDbConnection con = new OleDbConnection(CSVFileConnectionString))
                    {
                        con.Open();
                        var csvQuery = string.Format("select * from [{0}]", file.Name);
                        using (OleDbDataAdapter da = new OleDbDataAdapter(csvQuery, con))
                        {
                            da.Fill(dt);
                        }
                    }

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLServerConnectionString))
                    {
                        bulkCopy.ColumnMappings.Add(0, "MyGroup");
                        bulkCopy.ColumnMappings.Add(1, "ID");
                        bulkCopy.ColumnMappings.Add(2, "Name");
                        bulkCopy.ColumnMappings.Add(3, "Address");
                        bulkCopy.ColumnMappings.Add(4, "Country");
                        bulkCopy.DestinationTableName = "AllEmployees";
                        bulkCopy.BatchSize = 0;
                        bulkCopy.WriteToServer(dt);
                        bulkCopy.Close();
                    }

                }
                catch(Exception ex)
                     {
                         MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     }
            }
        }
    }
}

The CsvHelper NuGet library has an implementation for IDataReader which you can pass into the BulkCopy WriteToServer method. CsvHelper NuGet 库具有 IDataReader 的实现,您可以将其传递给 BulkCopy WriteToServer 方法。 This makes for really simple code and allows you to customize the data import.这使得代码非常简单,并允许您自定义数据导入。

using CsvHelper;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Globalization;
using System.IO;

public int ReplaceTableWithFile(string table, FileInfo csvFile)
{
    using var fileReader = new StreamReader(csvFile.OpenRead());
    using var csv = new CsvReader(fileReader, CultureInfo.InvariantCulture);
    using var csvDataReader = new CsvDataReader(csv);

    var connection = GetDbConnection();

    using var command = new SqlBulkCopy(connection);
    command.EnableStreaming = true;
    command.DestinationTableName = table;
    command.WriteToServer(csvDataReader);
    return command.RowsCopied;
}

CsvDataReader from CsvHelper NuGet package 来自 CsvHelper 的 CsvDataReader NuGet package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM