+ -
当前位置:首页 → 问答吧 → ASP如何将txt导入到sql server中

ASP如何将txt导入到sql server中

时间:2011-08-12

来源:互联网

如题,我要将txt中的内容插入到sql server中,请问如何做?(最好有代码)
txt的位置f:\aa.txt
txt中的内容:
1,张三,男,湖北,武汉大学,学生
2,李四,男,山东,湖北大学,学生
3,王五,男,河南,武汉大学,学生
  .
  .
  .
记录的条数有上万条

我要将这些内容分别导入到数据库(user)中的people表中,
表的设计为:
num int(4) 不允许空
name char(10) 允许空
sex char(8) 允许空
nationality char(10) 允许空
position char(20) 允许空
status char(10) 允许空

请问在ASP中该如何实现?

作者: fancyhf2009   发布时间: 2011-08-12

VBScript code

<%
sConn = "Provider=SQLOLEDB;Data source=127.0.0.1;Initial Catalog=db_name;User ID=sa;Password=sa;"
Set oConn = CreateObject("ADODB.Connection")
oConn.Open sConn
Set oRS = CreateObject("ADODB.RecordSet")
oRS.CursorLocation = 3
oRS.Open "SELECT * FROM people WHERE 1=0", oConn, 1, 4

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("F:\aa.txt", 1)
Do While Not oFile.AtEndOfStream
    line = Trim(oFile.ReadLine())
    If line <> "" Then
        a = Split(line, "")
        If UBound(a) >= 5 Then
            oRS.AddNew
            oRS("num").Value = CInt(a(0))
            oRS("name").Value = a(1)
            v = Trim(a(2))
            If v = "" Then v = Null
            oRS("sex").Value = v
            v = Trim(a(3))
            If v = "" Then v = Null
            oRS("nationality").Value = v
            v = Trim(a(4))
            If v = "" Then v = Null
            oRS("position").Value = v
            v = Trim(a(5))
            If v = "" Then v = Null
            oRS("status").Value = v
        End If
    End If
Loop
oFile.Close
Set oFile = Nothing
Set oFSO = Nothing
oRS.UpdateBatch
oRS.Close
Set oRS = Nothing
oConn.Close
Set oConn = Nothing
%>

作者: hookee   发布时间: 2011-08-12