如何从文件夹中读取文本文件并将其转换为MySQL中的表
本文关键字:转换 MySQL 文件 文件夹 读取 取文本 | 更新日期: 2023-09-27 18:01:02
我有一个包含50个文本文件的文件夹,我需要一个C#或.NET程序来读取这些文本文件并将其转换为表,我希望主键是文本文件本身的名称。
//sample contents of my 1.txt file is as follows
atro
astrology
king
moon
monkey
seven
skin //
所有文本文件都包含相同格式的信息。我写了一个宏,可以读取上面提到的数据格式的文本文件,然后当我试图在Excel中运行宏时,我遇到了一个错误,说明内存不足。
enter code here
子框架c((''ramesh宏''键盘快捷键:Ctrl+k'将nxt_row减为长
'Change Path
Const strPath As String = "C:'Users'roo'Desktop'Volumes'eGo'tags'0'"
Dim strExtension As String
'Stop Screen Flickering
Application.ScreenUpdating = False
ChDir strPath
'Change extension
strExtension = Dir(strPath & "*.txt")
Do While strExtension <> ""
'Adds File Name as title on next row
Range("A65536").End(xlUp).Offset(1, 0).Value = strExtension
'Sets Row Number for Data to Begin
nxt_row = Range("A65536").End(xlUp).Offset(1, 0).Row
'Below is from a recorded macro importing a text file
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & strPath & strExtension, Destination:=Range("$A$" & nxt_row))
.Name = strExtension
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
'Delimiter Settings:
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = True
.TextFileOtherDelimiter = "="
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub子框架((''ramesh宏''键盘快捷键:Ctrl+l'选择。复制ActiveCell。偏移(0,1(。范围("A1"(。选择Selection.PasteSpecial粘贴:=xlPasteAll,操作:=xlNone,SkipBlanks:=_False,Transpose:=True结束子
最快的方法是:
- 创建控制台应用程序。在文件夹中搜索文件。http://msdn.microsoft.com/en-us/library/vstudio/ezwyzy7b.aspx
- 使用System.Data.SqlClient建立直接连接。http://www.bigresource.com/MS_SQL--Problem-with-simple-sqlclient-database-acces--AVufrVBE.html
- 编写插入语句。迭代你的文件。并为每个插入一个。http://www.w3schools.com/sql/sql_insert.asp
你将使用这样的东西来读取你的文件:
string[] lines = File.ReadAllLines("C:/YourFile.txt");
foreach (var line in lines)
{
*Insert Statement*
}