 |
|
พบปัญหาเรื่อง Insert excel เข้า sql database แต่ได้ข้อมูลมาไม่ครบครับ ขอคำขี้แนะหน่อยครับ |
|
 |
|
|
 |
 |
|
photo excel original

ใน table จะเห็นว่าถ้า id ที่มี - จะไม่สามารถกวาดเข้า table ได้ขอคำชี้แนะหน่อยครับ ว่าจะ insert ข้อมูลเข้า table ได้อย่างไรให้ข้อมูลตรงกับ Excel ขอคำชี้แนะหน่อยครับ งมว่าทั้งวันละฮาๆๆๆๆๆๆๆ ขอบคุณล่วงหน้าครับ

Code (VB.NET)
Try
Dim sSourceConstr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", sPath)
Dim sDestConstr As String = ConfigurationManager.ConnectionStrings("dbProject").ConnectionString
Dim sSourceConnection As New OleDbConnection(sSourceConstr)
Using sSourceConnection
Dim sql As String = String.Format("Select [ProofNo],[PartName],[Customer],[ProductID],[Thickness],[Width],[Length],[Length1],[TypeFilm],[MD],[TD],[FilmWidth],[CylinderNo],[CylinderJointNo],[DirectPrinting],[Printingcolor],[Mold],[FinishGood],[RollLength],[CoreZise],[Seaming],[TapeColor],[DirectPacking],[Other],[Responsible],[PrepareBy],[Remark] FROM [{0}]", "Sheet1$")
Dim command As New OleDbCommand(sql, sSourceConnection)
sSourceConnection.Open()
Using dr As OleDbDataReader = command.ExecuteReader()
Using bulkCopy As New SqlBulkCopy(sDestConstr)
bulkCopy.DestinationTableName = "ProductSpecification"
'You can mannualy set the column mapping by the following way.
'bulkCopy.ColumnMappings.Add("Employee ID", "Employee Code");
bulkCopy.WriteToServer(dr)
End Using
End Using
End Using
Catch ex As Exception
Tag : .NET, Web (ASP.NET), VB.NET
|
|
 |
 |
 |
 |
Date :
2017-03-08 16:51:17 |
By :
adminliver |
View :
958 |
Reply :
11 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เช็ค datatype ดูนะครับ ว่าเป็น nchar/nvarchar รึป่าว
ถ้าใช่ก็ลองเปลี่ยนเป็น varchar ดูครับ
|
 |
 |
 |
 |
Date :
2017-03-08 17:10:45 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดึงข้อมูลจาก Excel เอามาแสดงใน datagridview ดูก่อนครับ
ในบางครั้งการดึงข้อมูลผ่าน oledb ข้อมูลที่ได้มามันผิดครับ
|
 |
 |
 |
 |
Date :
2017-03-08 17:10:55 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 5 เขียนโดย : lamaka.tor เมื่อวันที่ 2017-03-08 19:08:42
รายละเอียดของการตอบ ::
Code (VB.NET)
*******code upload excel ครับ
If FileUpload1.HasFile Then
Dim sPath As String = Server.MapPath("~/MyXls/" + FileUpload1.FileName)
FileUpload1.SaveAs(sPath)
ImporttoSQL(sPath)
End If
Server.Transfer("FrmSpecProduct.aspx")
Code (VB.NET)
********code insert excel ครับเป็นแบบ select copy นะครับ
Private Sub ImporttoSQL(sPath As String)
Try
Dim sSourceConstr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", sPath)
Dim sDestConstr As String = ConfigurationManager.ConnectionStrings("DBProject").ConnectionString
Dim sSourceConnection As New OleDbConnection(sSourceConstr)
Using sSourceConnection
Dim sql As String = String.Format("Select [ProofNo],[PartName],[Customer],[ProductID],[Thickness],[Width],[Length],[Length1],[TypeFilm],[MD],[TD],[FilmWidth],[CylinderNo],[CylinderJointNo],[DirectPrinting],[Printingcolor],[Mold],[FinishGood],[RollLength],[CoreZise],[Seaming],[TapeColor],[DirectPacking],[Other],[Responsible],[PrepareBy],[Remark] FROM [{0}]", "Sheet1$")
Dim command As New OleDbCommand(sql, sSourceConnection)
sSourceConnection.Open()
Using dr As OleDbDataReader = command.ExecuteReader()
Using bulkCopy As New SqlBulkCopy(sDestConstr)
bulkCopy.DestinationTableName = "ProductSpecification"
bulkCopy.WriteToServer(dr)
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
|
 |
 |
 |
 |
Date :
2017-03-08 19:28:44 |
By :
adminliver |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่มีตัวอย่างโดยตรงน่ะครับ
ผมไปก๊อปมาผสมกันให้น่ะครับ
คิดว่าน่าจะประมาณนี้
Code (VB.NET)
Dim oleExcel As New OleDbConnection
Dim oleExcelCmd As New OleDbCommand
Dim oleExcelWrt As New OleDbDataAdapter
Dim _Header As String = ""
Dim ExPath As String
dim TableName as string
_Header = "YES"
ExPath ="C:\1.xlsx"
TableName ="sheet1$"
oleExcel = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ExPath & ";Extended Properties=""Excel 12.0 Xml;HDR=" & _Header & ";IMEX=1"";")
oleExcel.Open()
oleExcelWrt = New OleDbDataAdapter
oleExcelCmd.CommandText = "select * from [" & TableName & "]"
oleExcelCmd.Connection = oleExcel
oleExcelWrt.SelectCommand = oleExcelCmd
dt = New DataTable
oleExcelWrt.Fill(dt)
oleExcel.Close()
for each dr as datarow in dt.rows
cmd="insert into Table1(Col1,Col2,Col3) Values('"
cmd+= dr(0) + "','"
cmd+= dr(1) + "','"
cmd+= dr(2) + "' ) "
'Conn = ตัวต่อกับ MS SQL
Conn.ExecuteNonQuery(cmd)
next
|
 |
 |
 |
 |
Date :
2017-03-09 10:07:34 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|