[英]Import .txt file to SQL database using VB.NET
I have a SQL database (local using vb.net) which has a table with 76 columns. 我有一个SQL数据库(使用vb.net在本地),其中有一个包含76列的表。 The data that needs to be put into those columns is in the form of a plain delimited text file.
需要放入这些列的数据采用纯定界文本文件的形式。 I need to build a VB.NET application which will allow me to import the text file into the table in the database under their appropriate columns.
我需要构建一个VB.NET应用程序,该应用程序允许我将文本文件导入数据库中相应列下的表中。 Is there any way I can do this?
有什么办法可以做到吗?
I'm very new to VB.NET. 我是VB.NET的新手。 Can someone help me out with the code?
有人可以帮我解决代码吗?
Thank You! 谢谢! Kamall
Kamall
If you have comma-separated values: 如果您有逗号分隔的值:
bulk insert tableName
from 'C:\myfile.txt'
with (fieldterminator = ',', rowterminator = '\n')
go
For tab-separated values use: 对于制表符分隔的值,请使用:
bulk insert tableName
from 'C:\myfile.txt'
with (fieldterminator = ',', rowterminator = '\n')
go
76 columns ? 76列
Must be the god-table antipattern ... 必须是神桌的反模式...
Public Sub CopyToDataBase(dt As DataTable)
Using Conn As SqlConnection = New SqlConnection("YOUR_CONNECTION_STRING")
Conn.Open()
Using s As SqlBulkCopy = New SqlBulkCopy(Conn)
s.DestinationTableName = "TableName"
s.WriteToServer(dt)
s.Close()
End Using
Conn.Close()
End Using
End Sub
of course this requires the table to have a primary key. 当然,这需要表具有主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.