简体   繁体   English

将嵌入式的CSV文件导入到SQL Server数据库

[英]Import embedded csv file to a SQL Server database

I am working on some code that creates a new database which I then want to populate with some required data. 我正在使用一些代码来创建一个新数据库,然后用一些必需的数据填充该数据库。 I have written a method MySqlCommand that connects to SQL Server and sends it SQL instructions. 我已经编写了一个MySqlCommand方法,该方法连接到SQL Server并向其发送SQL指令。 I wrote the code below to check that I can import the required data from a .csv file on my hard disk and this all works fine: 我编写了以下代码,以检查是否可以从硬盘上的.csv文件导入所需数据,并且一切正常:

MySqlCommand("BULK " +
             "INSERT Competition " +
             "FROM 'D:\\Users\\Stephen\\Documents\\Competition.csv'" +
             "WITH (" +
             "FIELDTERMINATOR = ','," +
             "ROWTERMINATOR = '\n')");

Now I have added the .csv file as an embedded resource in my Visual Studio project and I wish to import the data to the database from there. 现在,我已经将.csv文件添加为Visual Studio项目中的嵌入式资源,并且希望从那里将数据导入数据库。 I have figured out that I can access the embedded resource using: 我发现可以使用以下方式访问嵌入式资源:

var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream("Competition.csv");

I am now stuck though as I do not know how I might allow SQL Server to access the embedded resource data and I suspect I need to approach this differently. 我现在陷入了困境,因为我不知道如何允许SQL Server访问嵌入式资源数据,我怀疑我需要以不同的方式进行处理。 I would appreciate some help. 我将不胜感激。

I had a project similar, 我有一个类似的项目

what i did was add the items to a data table and gave it strict values. 我所做的就是将项目添加到数据表中,并赋予它严格的值。

then i looped through each row and inserted it, 然后我遍历每一行并将其插入,

this took a long time. 这花了很长时间。

The reason i couldnt do a bulk insert was because the csv was on my local machine and the sql server was on another remote place. 我无法进行批量插入的原因是因为csv在我的本地计算机上,而sql服务器在另一个远程位置。

If you have sql on your machine, the insert command looks something like this, 如果您的计算机上有sql,则insert命令如下所示:

BULK
INSERT myCSVFile
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO

OK, so I figured out a way to do this and thought I would post the answer to help others: 好的,因此我想出了一种方法来做,并认为我会发布答案以帮助他人:

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BetfairDatabaseMDI.embeddedResources.Competition.csv"))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        while (!reader.EndOfStream)
                        {
                            string result = reader.ReadLine();
                            result = "'" + result + "'";
                            result = result.Replace(",", "','");

                            MySqlCommand("INSERT INTO Competition " +
                                        "VALUES (" + result + ")");
                        }
                    }
                }

The idea I had was to read the embedded resource line by line and send the instructions to add each line to the SQL table 1 by 1. I guess its not as efficient as a method using bulk insert but its a one off command so not a problem in my case. 我的想法是逐行读取嵌入的资源,并发送指令以1乘1的方式将每一行添加到SQL表。我的问题。

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

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