使用IMEX选项
http://support.microsoft.com/default.aspx?scid=kb;EN-US;194124

解决方案 »

  1.   

    建议使用bulk insert 不要用"." 作为字段分隔符,最好是"," 例如:
    BULK INSERT Northwind.dbo.[Order Details]
       FROM 'c:\test.csv'
       WITH 
          (
             FIELDTERMINATOR = ',',  --使用逗号","作为字段分隔符
             ROWTERMINATOR = '\n'   --使用回车换行作为行分隔符
          )已经测试,没有问题
      

  2.   

    数据不是我导出的,没有办法加引号我只能导入这个文件用 zhangyang555(张阳) 的方法没有成功,报第 1 行、第 12 列(up_trunk_num)发生大容量插入数据转换错误(类型不匹配)。ROWTERMINATOR 换了\n和\r及他们的组合,还是这个问题,只是行号换了,郁闷啊
      

  3.   

    导入文本文件时如何指定字段类型?===========================================================================
    问题:
    我有一个文本文件需要导入 Access ,但是文本文件中有一列数据原本是文本,
    但是导入数据库后自动变成了“双精度”类型,我该如何让各个字段按我需要的
    数据类型生成哪?如何让 ACCESS 按照我规定的构架、规格从文本文件、XLS文档中导入数据? ==========================================================================
    方法:
    用 Schema.ini 文件作用:Schema.ini用于提供文本文件中记录的构架信息。
    每个 Schema.ini 项都用于指定表的五个特征之一:
    1、文本文件名
    2、文件格式
    3、字段名、字段长度、字段类型
    4、字符集
    5、特别数据类型转换======================================================================
    指定文件名
    文件名要用方括号括起来,例如如果要对 Sample.txt 使用数据构架信息文件,
    那么它的对应的项应该是
    [Sample.txt] 
    =======================================================================
    指定文件格式
    格式说明         表格式                Schema.ini 格式描述 
    Tab 制表符分隔   文件中的字段用制表符分隔     Format=TabDelimited 
    CSV 分隔         文件中的字段用逗号来分隔    Format=CSVDelimited 
    自定义分隔       文件中的字段可以用任何字符   Format=Delimited(自定义分隔符)
                     来分隔,所有的字符都可以
                     用来分隔,包括空格,但是
                    双引号 ( " ) 除外 
                    - 或者没有分隔符 -           Format=Delimited( )
    固定宽度         文件中的字段为固定长度  
    ===============================================================
    指定字段你可以有两种方法在一个字符分隔的文本文件中指定字段名1、在文本文件中的第一行包含字段名,并且设置 ColNameHeader 为 True 。
    2、用数字编号指定每一列并且指定每一列的名字以及数据类型你必须用数字编号指定每一列并且指定每一列的名字、数据类型以及长度
    (在固定长度分隔的文本文件中需要指定长度)注意,设定了 ColNameHeader 选项,在 Schema.ini 中 Windows 注册时会忽略
    FirstRowHasNames 选项。你也可以指定字段的数据类型,使用 MaxScanRows 选项用来指定在确定列的
    数据类型时要扫描多少行数据。设置 MaxScanRows 为 0 将扫描整个文件。如果文本文件第一行包含字段名,并且要扫描整个文件,改项目就要定义如下:
    ColNameHeader=True
    MaxScanRows=0 接下来的项目用来指定表中的字段,使用列编号(Coln)选项来指定列。字段长度在
    “固定分隔文本文件中”是必填项目,在“字符分隔文本文件”中是可选项目。示例:定义 2 个字段,CustomerNumber 是长度为 10 的文本字段、
    CustomerName 是长度为 30 的文本字段。Col1=CustomerNumber Text Width 10
    Col2=CustomerName Text Width 30 
    语法如下:
    Coln=ColumnName type [Width #]参数解释如下:
    参数 说明 
    ColumnName 文本,标识字段名,如果包含空格要用双引号括起来 
    type 数据类型包括:
    Microsoft Jet 数据类型:Bit Byte Short Long Currency Single Double DateTime Text MemoODBC 数据类型: Char (same as Text) Float (same as Double) Integer (same as Short) 
    LongChar (same as Memo) Date date format其中date format 是日期的格式字符串例如:Date YYYY-MM-DD 
     
    Width 字符串的长度,后面的数字用来指定字段的长度(“固定分隔文本文件”为必填,
    “文字分隔文本文件”为可选) 
    # 整形数字,标识字段长度 
    ===================================================================
    指定字符集
    CharacterSet 项有两个选择:ANSI | OEM
    选择 ANSI 字符集用如下方法:
    CharacterSet=ANSI ====================================================================
    特别数据类型转换 
    特别数据类型转换主要是定义比如日期、货币型数据如何转换或者如何显示的,
    你可以参考下面这张表:
    选项 说明 
    DateTimeFormat
     Can be set to a format string indicating dates and times. You should specify this entry if all date/time fields in the import/export are handled with the same format. All Microsoft Jet formats except A.M. and P.M. are supported. In the absence of a format string, the Windows Control Panel short date picture and time options are used.
     
    DecimalSymbol
     Can be set to any single character that is used to separate the integer from the fractional part of a number. 
     
    NumberDigits
     Indicates the number of decimal digits in the fractional portion of a number. 
     
    NumberLeadingZeros
     Specifies whether a decimal value less than 1 and greater than –1 should contain leading zeros; this value can either be False (no leading zeros) or True. 
    CurrencySymbol
     Indicates the currency symbol to be used for currency values in the text file. Examples include the dollar sign ($) and Dm.
     
    CurrencyPosFormat
     Can be set to any of the following values: 
    · Currency symbol prefix with no separation ($1) · Currency symbol suffix with no separation (1$) · Currency symbol prefix with one character separation ($ 1) · Currency symbol suffix with one character separation (1 $)  
    CurrencyDigits
     Specifies the number of digits used for the fractional part of a currency amount. 
     
    CurrencyNegFormat
     Can be one of the following values: 
    · ($1) · –$1 · $–1 · $1– · (1$) · –1$ · 1–$ · 1$– · –1 $ · –$ 1 · 1 $– · $ 1– · $ –1 · 1– $ · ($ 1) · (1 $) This example shows the dollar sign, but you should replace it with the appropriate CurrencySymbol value in the actual program. 
    CurrencyThousandSymbol
     Indicates the single-character symbol to be used for separating currency values in the text file by thousands.
     
    CurrencyDecimalSymbol
     Can be set to any single character that is used to separate the whole from the fractional part of a currency amount. 
     
     
    ====================================================================
    下面给出一个简单的例子,假设有一个表Contacts.txt类似下面:
    姓名 单位 联系日期 
    王海 上海有机化学研究所 2002-1-1 
    罗炙 数字化机床研究院 2004-1-1  导入 Access 应该类似下面表格:
    姓名 单位 联系日期 
    王海 上海有机化学研究所 2002-1-1 
    罗炙 数字化机床研究院 2004-1-1 那么 Schema.ini 则是类似下面的INI文件:
    [Contacts.txt]
    ColNameHeader=True
    format=Delimited(" ")
    MaxScanRows=0
    CharacterSet=ANSI
    Col1="姓名" Char Width 10
    Col2="单位" Char Width 9
    Col3="联系日期" Date Width 8 
    注释如下:
    [Contacts.txt] ///文本文件名
    ColNameHeader=True ///带有表头
    format=Delimited( ) ///空格作为分隔符,如果是分号,请用format=Delimited(;) 来解决
    MaxScanRows=0 ///扫描整个文件
    CharacterSet=ANSI ///ANSI 字符集
    Col1="姓名" Char Width 10 ///字段1
    Col2="单位" Char Width 9 ///字段2
    Col3="联系日期" Date Width 8 ///字段3
    ///如果有更多字段可 Col4 .... ColN  
    注意,Schema.ini 必须和需要导入的文本文件在同一目录。此后,我们就可以利用下面的语句来导入数据了:CurrentProject.Connection.Execute "SELECT * INTO NewContact FROM [Text;FMT=Delimited;HDR=Yes;DATABASE=C:\;].[Contacts#txt];" 
      

  4.   

    根据上述描述, 构建 schema.ini 文件就可以了.
      

  5.   

    方法果然好,真的导入IP地址了,最后一个字段还是认不出,这已经不是大问题了现在问题是,虽然这个方法可以导入IP地址,但需要手动新增一个schema.ini,而且需要在里面指定文件名。在实际应用中,所有导入流程都将写成JOB,让数据库定时自动执行,而导入文件的文件名是随时间变化而变化的,我该怎么办呢?