简体   繁体   English

如何将txt文件中的每一行插入到SQL Server数据库的单独行中?

[英]How can I insert each line from txt file into separate rows in a SQL Server database?

I have a text file which has words from dictionary like.. 我有一个文本文件,其中包含字典中的单词。

ABACA
ABACHI
ABACHISTA
ABACO
ABADESSA
ABADIA
ABALIETA
ABARICA

Now what I want to do is insert this list from a text file into a SQL Server table. 现在,我要做的是将此列表从文本文件插入到SQL Server表中。 I can not possibly add quotes manually. 我可能无法手动添加引号。 I think one way of doing this could be copy pasting this into Excel sheet and then importing it or adding quotes but Excel sheet has like 65k rows in a sheet and this text file has much greater number of words in it. 我认为这样做的一种方法是将其复制粘贴到Excel工作表中,然后将其导入或添加引号,但是Excel工作表中的工作表中有65k行,并且此文本文件中的单词数量更多。 So how can this be done easily? 那么如何轻松地做到这一点呢? I am using SQL Server 2008. 我正在使用SQL Server 2008。

You can easily bulk import a huge file with a lot of words using T-SQL - just create a table with a single column of type varchar , long enough to hold your longest string, and then bulk insert your data. 您可以使用T-SQL轻松地批量导入包含多个单词的文件-只需创建一个表,该表的类型为varchar类型,长度足以容纳最长的字符串,然后批量插入数据。

Try to use T-SQL statements something like this: 尝试使用T-SQL语句,如下所示:

CREATE TABLE ImportedWordList(Word VARCHAR(200) )

BULK INSERT ImportedWordList
FROM 'c:\temp\listofwords.txt'
WITH
(ROWTERMINATOR = '\n')
GO

SELECT * FROM dbo.ImportedWordList

Works like a charm in my limited testing (and takes about 20 seconds to load a list of over a million words into that table). 在我有限的测试中,它的工作原理就像一个魅力(大约需要20秒才能将超过一百万个单词的列表加载到该表中)。

As you've tagged your question with C#, Just use: 使用C#标记问题后,只需使用:

var allLines = File.ReadAllLines(filepath);

to get all of the lines, run on all of them with a foreach and use whatever way you want to insert it to your database, You can use: 要获取所有行,使用foreach在所有行上运行,并使用您想将其插入数据库的任何方式,可以使用:

using (var con = new SqlConnection(connectionString))
{
    foreach (var line in allLines)
    {
        using (var cmd = con.CreateCommand())
        {
            cmd.CommandText = "INSERT INTO [YourTable] ([YourColumn]) VALUES (:val)";

            var param = cmd.CreateParameter();
            param.ParameterName = ":val";
            param.Value = line;

            cmd.ExecuteNonQuery();
        }
    }
}

I haven't tested that but it's simple enough to succeed with not much trouble. 我还没有测试过,但这很简单,没有太多麻烦就可以成功。

I geuss it'll be better to send all the inserts in a single command, but that's just a poc 我猜想最好是在单个命令中发送所有插入,但这只是一个POC

Do you only have to do this once? 您只需要这样做一次吗?

Open up management studio and use Import Data 打开管理工作室并使用导入数据

choose Flat File Source as the data source 选择平面文件源作为数据源

This could be an example. 这可能是一个例子。

string str = null;
StreamReader sr = new StreamReader(YourTextFilePath);
using (var con = new SqlConnection(YourConnectionString))
{
    while ( (str = sr.ReadLine()) != null)
    {
        cmd = new SqlCommand("Insert into table_name values ('" + str + "')", con);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
    }
}

My code might be wrong/inefficient. 我的代码可能是错误的/效率低下。 So, feel free to correct me. 因此,随时纠正我。 ;) ;)

BULK INSERT TABLE_NAME FROM 'file path' WITH (ROWTERMINATOR = '\\n') 从'文件路径'WITH(ROWTERMINATOR ='\\ n')批量插入TABLE_NAME

* When you giving the path You should give server path.otherwise query will get error * * 提供路径时,应提供服务器路径,否则查询将出错*

暂无
暂无

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

相关问题 如何在 TXT 中打印数据库的每一行? C# - How can I print each line of a database in a TXT? c# 将大量数据从 txt、csv 文件插入到 SQL 服务器表中,无效行数很少 - Insert large size of data from txt, csv file to SQL Server table with few invalid rows 如何读取txt文件并将其导入到sql数据库? - How can i read txt file and import it to sql database? 如何在比较数据库中的每一行时有效地使用 C# 在 SQL 服务器中插入/更新 10000 行 - How to insert/Update 10000 rows in SQL Server using C# efficiently while comparing each row from database 如何使用 C# 向 SQL Server 插入/更新行 - How can I insert/update rows with C# to SQL Server 如何以编程方式对 C# SQL Server 数据库进行排序并将每个项目显示为单独的按钮? - How do I sort a C# SQL Server database programmatically and display each item as a separate button? 从 .txt 文件向 Sql 数据库添加值,跳过第一行 - Add values to a Sql Database from a .txt file, skipping the first line 使用C#,EF和SQL Server,仅将不存在的行从Excel文件插入数据库 - Insert into database from an Excel file only rows that don't exist, using C#, EF, and SQL Server 如何从数据库中读取txt文件(逐行) - how to read the txt file from the database(line by line) 从文本行读取文本文件,然后使用C#插入SQL Server数据库 - Read text file from text line and insert into SQL Server database using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM