-1
嗨我導入CSV文件到SQL使用vb.net 和現在我不知道我應該使用ControlChars字段。ControlChars Fields
每當我用ControlChars.Lf
我的SQL腳本是這樣的:
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'47_8', N'NC56', N'J93
')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'61_3', N'NC60', N'F123
')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'63_8', N'NC61', N'J125
')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'16_7', N'ND8', N'B29
')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'29_1', N'ND9', N'F58
')
有在值的末尾換行。
雖然,每當我用ControlChars.CrLf和ControlChars.Cr
劇本是這樣的:
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'
47_8', N'NC56', N'J93')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'
61_3', N'NC60', N'F123')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'
63_8', N'NC61', N'J125')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'
16_7', N'ND8', N'B29')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'
29_1', N'ND9', N'F58')
新線的第一個值。
現在我不知道該怎麼ControlChars場我應該使用這個腳本:
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'47_8', N'NC56', N'J93')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'61_3', N'NC60', N'F123')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'63_8', N'NC61', N'J125')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'16_7', N'ND8', N'B29')
INSERT [dbo].[TblR8Mix] ([WireID], [PadNo], [Pattern], [Tester]) VALUES (8, N'29_1', N'ND9', N'F58')
在我的數據域的每個值沒有新線。
我的VB.NET代碼:
Dim csvPath As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(csvPath)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("WireID", GetType(String)), New DataColumn("PadNo", GetType(String)), New DataColumn("CoaxSolid", GetType(String)), New DataColumn("Pattern", GetType(String)), New DataColumn("Tester", GetType(String))})
Dim csvData As String = File.ReadAllText(csvPath)
For Each row As String In csvData.Split(ControlChars.Cr)
If Not String.IsNullOrEmpty(row) Then
dt.Rows.Add()
Dim i As Integer = 0
Dim wireid As Integer = ID.ToString
For Each cell As String In row.Split(",")
If i = 0 Then
dt.Rows((dt.Rows.Count - 1))(i) = wireid
dt.Rows((dt.Rows.Count - 1))(1) = cell
Else
dt.Rows((dt.Rows.Count - 1))(i + 1) = cell
End If
i += 1
Next
End If
Next
Using con As New SqlConnection(ConStrX)
Using sqlBulkCopy As New SqlBulkCopy(con)
sqlBulkCopy.DestinationTableName = "dbo.TblR8Mix"
con.Open()
sqlBulkCopy.WriteToServer(dt)
con.Close()
End Using
End Using