|
|
|
ต้องการที่จะ import txtfile to database ต้องที่จะ import txtfile to database ต้องเขียน code แบบไหนค่ะ พอมีตัวอย่างมัยค่ะ |
|
|
|
|
|
|
|
Code (ASP Read CSV Import/Insert Ms Access)
<%Option Explicit%>
<html>
<head>
<title>ThaiCreate.Com ASP & Import CSV To Access</title>
</head>
<body>
<%
Dim objFSO,oInStream,sRows,arrRows
Dim Conn,strSQL,objExec
Dim sFileName
sFileName = "customer.csv"
'*** Create Object ***'
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*** Check Exist Files ***'
If Not objFSO.FileExists(Server.MapPath(sFileName)) Then
Response.write("File not found.")
Else
'*** Open Files ***'
Set oInStream = objFSO.OpenTextFile(Server.MapPath(sFileName),1,False)
'*** open Connect to Access Database ***'
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("mydatabase.mdb"),"" , ""
Do Until oInStream.AtEndOfStream
sRows = oInStream.readLine
arrRows = Split(sRows,",")
'*** Insert to table customer2 ***'
strSQL = ""
strSQL = strSQL &"INSERT INTO customer2 "
strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "
strSQL = strSQL &"VALUES "
strSQL = strSQL &"('"&arrRows(0)&"','"&arrRows(1)&"','"&arrRows(2)&"' "
strSQL = strSQL &",'"&arrRows(3)&"','"&arrRows(4)&"','"&arrRows(5)&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
Loop
oInStream.Close()
Conn.Close()
Set oInStream = Nothing
Set Conn = Nothing
End If
Response.write ("CSV Import to Access Done.")
%>
</table>
</body>
</html>
|
|
|
|
|
Date :
2010-05-22 08:23:49 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ช่วยดู code หน่อย ค่ะ
ทำแบบตัวอย่างที่ให้แล้ว
ยัง import ลง sql ไม่ได้เลย ไม่มี error ด้วยค่ะ code เป็นแบบนี้ค่ะ
<% Option Explicit %>
<html>
<head>
<title>Importdatatoexcel</title>
</head>
<body>
<%
Dim objFSO,oInStream,sRows,arrRows,i
Dim Conn,strSQL,objExec
Dim sFileName
Dim num
sFileName = "SAP_GI.txt"
'*** Create Object ***'
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*** Check Exist Files ***'
If Not objFSO.FileExists(Server.MapPath(sFileName)) Then
' Response.write("File not found.")
Else
'*** Open Files ***'
Set oInStream = objFSO.OpenTextFile.Open("C:\SAP_GI.txt")
'Set oInStream = objFSO.OpenTextFile(Server.MapPath(sFileName),1,False)
SET CONN = SERVER.CREATEOBJECT("ADODB.CONNECTION")
CONN.OPEN "Provider=SQLOLEDB.1;Data Source=192.1.10.58;Initial Catalog=SAP_MM;User ID=nok;Pwd=nok"
'SET RS = SERVER.CREATEOBJECT("ADODB.RECORDSET")
Do Until oInStream.AtEndOfStream
sRows = oInStream.readLine
arrRows = Split(sRows,",")
num = num+1
strSQL = ""
strSQL = strSQL &" INSERT INTO SAP_GI "
strSQL = strSQL &" (Posting_Date,Document_Header_Text,Material,Material_Description,Qty_in_Un_Of_Entry,Unit_of_Entry,Batch,Cost_Center,Movement_Type,Storage_Location,Material_Document,Special_Stock,Vendor,Amount_in_LC,GL_Account)"
strSQL = strSQL &" VALUES "
strSQL = strSQL &"('"&arrRows.Item(i,0)&"','"&arrRows.Item(i,1)&"','"&arrRows.Item(i,2)&"' "
strSQL = strSQL &",'"&arrRows.Item(i,3)&"','"&arrRows.Item(i,4)&"','"&arrRows.Item(i,5)&"') "
strSQL = strSQL &",'"&arrRows.Item(i,6)&"','"&arrRows.Item(i,7)&"','"&arrRows.Item(i,8)&"') "
strSQL = strSQL &",'"&arrRows.Item(i,9)&"','"&arrRows.Item(i,10)&"','"&arrRows.Item(i,11)&"') "
strSQL = strSQL &",'"&arrRows.Item(i,12)&"','"&arrRows.Item(i,13)&"','"&arrRows.Item(i,14)&"') "
strSQL = strSQL &",'"&arrRows.Item(i,15)&"') "
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
Loop
oInStream.Close()
Conn.Close()
Set oInStream = Nothing
Set Conn = Nothing
End If
'Response.write ("CSV Import to sql Done.")
%>
|
|
|
|
|
Date :
2010-05-22 10:23:30 |
By :
nok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตรง array น่าจะผิด เพราะเป็นเพียง array 1 มิติ ธรรมดาครับ
arrRows.Item(i,0) ผิด
arrRows(0) ถูก
Code (ASP)
strSQL = strSQL &" VALUES "
strSQL = strSQL &"('"&arrRows(0)&"','"&arrRows(1)&"','"&arrRows(2)&"' "
strSQL = strSQL &",'"&arrRows(3)&"','"&arrRows(4)&"','"&arrRows(5)&"') "
strSQL = strSQL &",'"&arrRows(6)&"','"&arrRows(7)&"','"&arrRows(8)&"') "
strSQL = strSQL &",'"&arrRows(9)&"','"&arrRows(10)&"','"&arrRows(11)&"') "
strSQL = strSQL &",'"&arrRows(12)&"','"&arrRows(13)&"','"&arrRows(14)&"') "
strSQL = strSQL &",'"&arrRows(15)&"') "
|
|
|
|
|
Date :
2010-05-24 02:42:32 |
By :
luecha |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มันเขียนไม่ลง data base อ่ะค่ะ
ไม่มี error ด้วย
ช่วยดูcode ให้หน่อยค่ะ
ขอบคุณค่ะ
<% Option Explicit %>
<html>
<head>
<title>Importdatatoexcel</title>
</head>
<body>
<%
Dim objFSO,oInStream,sRows,arrRows,i
Dim Conn,strSQL,objExec
Dim sFileName
Dim num
sFileName = "SAP_GI.txt"
'*** Create Object ***'
Set objFSO = CreateObject("Scripting.FileSystemObject")
'*** Check Exist Files ***'
If Not objFSO.FileExists(Server.MapPath(sFileName)) Then
' Response.write("File not found.")
Else
'*** Open Files ***'
Set sFileName = objFSO.OpenTextFile("C:\SAP_GI.txt")
'Set oInStream = objFSO.OpenTextFile(Server.MapPath(sFileName),1,False)
SET CONN = SERVER.CREATEOBJECT("ADODB.CONNECTION")
CONN.OPEN "Provider=SQLOLEDB.1;Data Source=192.1.10.58;Initial Catalog=SAP_MM;User ID=nok;Pwd=nok"
'SET RS = SERVER.CREATEOBJECT("ADODB.RECORDSET")
Do Until oInStream.AtEndOfStream
sRows = oInStream.readLine
arrRows = Split(sRows,",")
num = num+1
strSQL = ""
strSQL = strSQL &" INSERT INTO SAP_GI "
strSQL = strSQL &" (Posting_Date,Document_Header_Text,Material,Material_Description,Qty_in_Un_Of_Entry,Unit_of_Entry,Batch,Cost_Center,Movement_Type,Storage_Location,Material_Document,Special_Stock,Vendor,Amount_in_LC,GL_Account)"
strSQL = strSQL &" VALUES "
strSQL = strSQL &"('"&arrRows(0)&"','"&arrRows(1)&"','"&arrRows(2)&"' "
strSQL = strSQL &",'"&arrRows(3)&"','"&arrRows(4)&"','"&arrRows(5)&"') "
strSQL = strSQL &",'"&arrRows(6)&"','"&arrRows(7)&"','"&arrRows(8)&"') "
strSQL = strSQL &",'"&arrRows(9)&"','"&arrRows(10)&"','"&arrRows(11)&"') "
strSQL = strSQL &",'"&arrRows(12)&"','"&arrRows(13)&"','"&arrRows(14)&"') "
strSQL = strSQL &",'"&arrRows(15)&"') "
response.Write(strSQL)
Set objExec = Conn.Execute(strSQL)
Set objExec = Nothing
Loop
oInStream.Close()
Conn.Close()
Set oInStream = Nothing
Set Conn = Nothing
End If
'Response.write ("CSV Import to sql Done.")
%>
|
|
|
|
|
Date :
2010-05-24 09:00:35 |
By :
nok |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|