ODBC方式读入txt文件『c#』

   " Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=C:\\DatabasePath\\;Extensions=asc,csv,tab,txt;" 
    
如果文本文件使用 tab 作为分隔符,你必须创建 schema.ini 文件,你必须在连接串中使用 Format=TabDelimited 选项。
    
注意:你必须在 sql 语句中指定文件名(例如 "Select Name, Address From Clients.txt"

c#】:

例:table_A,结构如下

字段1     数值  
字段2     字符串  
字段3     日期  

Clients.txt 文本格式如下:

字段1        字段2        字段3  
  1,"
张三",2001/01/02  
  2,"
李四",2001/01/03  
  3,"
王五",2001/03/02  

 

String filename="Clients";       //   filename是读入txt文件名

String ServerPath="c:\\";        // ServerPath存放文件的路径

constr="Driver={Microsoft Text Driver (*.txt; *.csv)}; Dbq=" + ServerPath+";Extensions=asc,csv,tab,txt;";

OdbcDataAdapter da=new OdbcDataAdapter ();

OdbcConnection OConn=new OdbcConnection ();

OConn.ConnectionString =constr;

OConn.Open ();

OdbcCommand OCmm=new OdbcCommand ();

OCmm.CommandText = "Select * from "+filename+".txt";   

OCmm.Connection=OConn;

da.SelectCommand =OCmm;

DataSet ds = new DataSet ();

try

{

da.Fill(ds,filename);

}

catch

{

OConn.Close();

Response.Write("<script language=\"\">window.alert(\"读入错误!\");</script>");

return ;

}

读入dataset后再处理读入数据表。

 

schema.ini

 

[Clients.txt]

ColNameHeader=true

Format=TabDelimited

CharacterSet=ANSI

Col1=字段1 integer

Col2=字段2 Char Width 8

Col3=字段3 date

 

注:

ColNameHeader,是否带有列标题:

ColNameHeader=true     带列标题(从第2行读入)      

ColNameHeader=false    不带列标题(从第1行读入)

Format,文本文件的分割符:

Format=TabDelimited是以tab键为分隔符,Format=Delimited(|)是以’|’为分隔符,Format=Delimited(;)是以’;’为分隔符等等

CharacterSet,是字符集:ANSI

Col+NChar Width 9integerdate

只能处理一种分隔符的,如有两种或两种以上的要先替换成同一分隔符再导入。

 

用代码创建schema.ini文件的方法:

声明:

[DllImport("kernel32")]
  private static extern long WritePrivateProfileString ( string section , string key , string val , string filePath ) ;
调用

WritePrivateProfileString("Clients.txt"," ColNameHeader","true", ServerPath+"\\schema.ini"); 

WritePrivateProfileString("Clients.txt"," Format"," TabDelimited ", ServerPath+"\\schema.ini"); 

WritePrivateProfileString("Clients.txt"," CharacterSet"," ANSI ", ServerPath+"\\schema.ini"); 

WritePrivateProfileString("Clients.txt"," col1","字段1 integer ", ServerPath+"\\schema.ini"); 

WritePrivateProfileString("Clients.txt"," col2","字段2 Char Width 8", ServerPath+"\\schema.ini"); 

WritePrivateProfileString("Clients.txt"," col3","字段3 date ", ServerPath+"\\schema.ini"); 

 

注意:WritePrivateProfileString("Clients.txt"," ColNameHeader","true", ServerPath+"\\schema.ini");

Clients.txt[]里的值,不能用变量+字符串的形式(重复出现[xxxxxx]),可以用变量或固定字符串

ColNameHeader:键值名

True:键值

ServerPath+schema.ini:带路径的ini文件

转载于:https://www.cnblogs.com/yequancheng/archive/2007/04/23/ODBC.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值