確定試試這個...它會導入CSV文件,拆分所需的字段併合並它。
我修改了代碼以在LINQ語句中包含RegEx來拆分該字段,如果沒有文件名,它將工作,但是在那裏必須有一個正斜槓並且這必須是唯一字段(在你的CSV文件中),其中包含正斜槓...原因是,如果正斜槓丟失,並且沒有文件名,你將丟失一個字段,或者如果csv文件中的其他字段包含斜槓.....
Sub Main()
Dim regex As Text.RegularExpressions.Regex = New Text.RegularExpressions.Regex("\d+")
Dim readText() As String = File.ReadAllLines("<Path to your CSV file>")
Using connection As SqlConnection = New SqlConnection("<DB connection string>")
Dim command As SqlCommand = connection.CreateCommand()
connection.Open()
command.CommandType = System.Data.CommandType.StoredProcedure
command.CommandText = "SP_InsertCSVData"
For i As Integer = 0 To readText.Count
Dim readRecord() As String = readText.ToArray
Dim CSVData As XElement = New XElement("Root",
From str In readText
Let fields = Text.RegularExpressions.Regex.Replace(str, "(by|on|\/(?=[^\/]*$))", ",").Split(",")
Select New XElement("CSVDataRecord",
New XAttribute("Field_1", fields(0)),
New XAttribute("Field_2", fields(1)),
New XAttribute("Field_3", fields(2)),
New XAttribute("Field_4", fields(3)),
New XAttribute("Field_5", fields(4)),
New XAttribute("Field_6", fields(5)),
New XAttribute("Field_7", fields(6)),
New XAttribute("Field_8", fields(7))
)
)
command.Parameters.Clear()
command.Parameters.Add(New SqlParameter With
{
.ParameterName = "@InputXML",
.DbType = DbType.Xml,
.Value = CSVData.CreateReader
})
command.ExecuteNonQuery()
Next
End Using
End Sub
這裏是新表...
/****** Object: Table [dbo].[CSV_Import_Table] Script Date: 06/02/2016 01:55:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CSV_Import_Table](
[Field_1] [nvarchar](max) NULL,
[Field_2] [nvarchar](max) NULL,
[Field_3] [nvarchar](max) NULL,
[Field_4] [nvarchar](max) NULL,
[Field_5] [nvarchar](max) NULL,
[Field_6] [nvarchar](max) NULL,
[Field_7] [nvarchar](max) NULL,
[Field_8] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
這裏是SP ...
/****** Object: StoredProcedure [dbo].[SP_InsertCSVData] Script Date: 06/02/2016 01:56:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SP_InsertCSVData] (@InputXML xml)
as
begin
MERGE CSV_Import_Table AS main
USING (select Row.id.value('@Field_1','[nvarchar](MAX)') as Field_1, Row.id.value('@Field_2','[nvarchar](MAX)') as Field_2, Row.id.value('@Field_3','[nvarchar](MAX)') as Field_3, Row.id.value('@Field_4','[nvarchar](MAX)') as Field_4, Row.id.value('@Field_5','[nvarchar](MAX)') as Field_5, Row.id.value('@Field_6','[nvarchar](MAX)') as Field_6, Row.id.value('@Field_7','[nvarchar](MAX)') as Field_7, Row.id.value('@Field_8','[nvarchar](MAX)') as Field_8
from @InputXML.nodes('/Root/CSVDataRecord') as Row(id)) as stage
ON main.Field_1=stage.Field_1
WHEN MATCHED THEN
UPDATE SET main.Field_1=stage.Field_1, main.Field_2=stage.Field_2, main.Field_3=stage.Field_3, main.Field_4=stage.Field_4 , main.Field_5=stage.Field_5 , main.Field_6=stage.Field_6, main.Field_7=stage.Field_7, main.Field_8=stage.Field_8
WHEN NOT MATCHED THEN
INSERT (Field_1, Field_2, Field_3, Field_4, Field_5, Field_6, Field_7, Field_8) VALUES (stage.Field_1, stage.Field_2, stage.Field_3, stage.Field_4, stage.Field_5, stage.Field_6, stage.Field_7, stage.Field_8);
end
而且我猜你CSV文件看起來是這樣的....
記錄1場1,通過DOMAIN \ USERLOGIN成功的寫上/ SOMENETWORKSHARE /文件夾/ MyFolder中/ USERLOGIN/DesktopBackUp /Log.txt,字段6,字段7,字段8
Google VB **子字符串()**或VB **正則表達式**。祝你好運 – mxix
我不認爲你提到*哪一條數據你想要的所有。 – Plutonix
我想將一個長字段名稱分成4個字段,我把它們分解到上面。 – Dcapers