怎样打开长文件名的.dbf文件
我用下面的方法打开dbf文件,如果文件名小于8位就没有问题,但文件名太长
就会出现系统错误,请问各位怎么解决这个问题,
或是怎样用OleDb连接dbf文件,希望给出代码.
string FileName="c:\\20030812-321.dbf";
string name=null;
FileInfo file=new FileInfo(FileName);
name=file.Name;//文件名
path=file.Directory.FullName+"\\";//文件目录
string strConnection="Driver={Microsoft dBASE Driver (*.dbf)};DriverID=227;Dbq="+path;
string strSelect="SELECT * FROM "+name;
OdbcConnection thisConnection=new Microsoft.Data.Odbc.OdbcConnection(strConnection);
thisConnection.Open();
OdbcDataAdapter thisAdapter=new OdbcDataAdapter(strSelect,thisConnection);
OdbcCommandBuilder thisBuilder=new OdbcCommandBuilder(thisAdapter);
DataSet thisDataSet=new DataSet();
name=name.Substring(0,name.Length-4);
thisAdapter.Fill(thisDataSet,name);//如果name的长度超过8位会有错误
int n=thisDataSet.Tables[0].Rows.Count;
MessageBox.Show(n.ToString ());
问题点数:100、回复次数:25Top
1 楼jhonsn(晓晨)回复于 2003-12-03 20:37:11 得分 3
upTop
2 楼wuyeniao(巷陌·清茶)回复于 2003-12-03 21:31:55 得分 3
thisAdapter.Fill(thisDataSet,name)中的name=?如果name="20030812-321.dbf"那么它在文件系统的名称可能与看到的并不一致,可能存在一些非法的(在VS)字符,以至于name作为string类型处理时出现错误?贴出你的错误来看哦Top
3 楼wuyeniao(巷陌·清茶)回复于 2003-12-03 21:33:24 得分 3
查看以name作为参数的函数说明Top
4 楼rock1981(呵呵呵)回复于 2003-12-03 21:48:42 得分 3
系统错误?具体一点...会不会有非法字符Top
5 楼redbb(....DB & DEV....抵制日货,人人有责...)回复于 2003-12-03 22:53:49 得分 3
server.urlencode() or name.replace()Top
6 楼gOODiDEA(无语)回复于 2003-12-04 07:39:30 得分 3
在这里中断看看name=name.Substring(0,name.Length-4);
出错的时候name的值是什么?
Top
7 楼gxz01()回复于 2003-12-04 13:31:31 得分 0
未处理的异常Microsoft.Data.Odbc.OdbcException出现在
microsoft.data.odbc.dll
其它信息:系统错误
中断以后检查文件名就是20030812-321这个文件名
如果我把文件名换为0812-321.dbf也会出错误,如果换为
0812a321.dbf就没有问题,如果再加多一个字母,就又
会出错,错误信息都是一样的,
好向是文件名不能大于8位数,并且不能包含"-"号",是不是
没有办法解决?Top
8 楼NetAnt007(飞呀飞)回复于 2003-12-04 23:44:29 得分 3
应该不是长度的问题,好像是有特殊的字符的问题,我在asp中也出现这样的问题,我只能告诉客户给文件命名的时候怎样怎样?Top
9 楼gxz01()回复于 2003-12-05 14:56:30 得分 0
如果用Oledb会不会出现这种问题?,我用Oledb打开Access文件时就没有这个问题.Top
10 楼gxz01()回复于 2003-12-05 17:50:17 得分 0
各位帮忙呀Top
11 楼wuyeniao(巷陌·清茶)回复于 2003-12-05 17:50:58 得分 3
另外,也可能是OleDB的问题吧,查询结果在服务器端生成临时表或者生成游标(API游标)时,可能用name作为该对象的名称,查看一下SQl等数据库中相关对象的命名规则,
帮你UPTop
12 楼NetAnt007(飞呀飞)回复于 2003-12-05 17:55:01 得分 3
有非法的字符吧。Top
13 楼rock1981(呵呵呵)回复于 2003-12-05 18:01:31 得分 3
upTop
14 楼gxz01()回复于 2003-12-08 13:45:22 得分 0
只要超过文件名超过8位就会错,Top
15 楼ldy(罗大佑)回复于 2003-12-09 09:16:54 得分 70
我试了,的确是超过8位就出错
我把文件名修改成123456789.dbf出错,改成12345678.dbf正确
====================================================
葬我于高山之上兮,望我大陆。大陆不可见兮,只有痛哭。
葬我于高山之上兮,望我故乡。故乡不可见兮,永远不忘。
天苍苍,野茫茫,山之上,国有殇。
====================================================Top
16 楼ldy(罗大佑)回复于 2003-12-09 09:17:34 得分 0
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
using System.IO;
namespace Sample
{
/// <summary>
/// frmDBFDemo 的摘要说明。
/// </summary>
public class frmDBFDemo : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnRetrieve;
private System.Windows.Forms.Button btnCancel;
private System.Data.Odbc.OdbcConnection odbcConnection1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmDBFDemo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnRetrieve = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.odbcConnection1 = new System.Data.Odbc.OdbcConnection();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(384, 280);
this.dataGrid1.TabIndex = 0;
//
// btnRetrieve
//
this.btnRetrieve.Location = new System.Drawing.Point(392, 224);
this.btnRetrieve.Name = "btnRetrieve";
this.btnRetrieve.TabIndex = 1;
this.btnRetrieve.Text = "&Retrieve";
this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(392, 256);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "&Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// frmDBFDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 285);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnRetrieve);
this.Controls.Add(this.dataGrid1);
this.Name = "frmDBFDemo";
this.Text = "frmDBFDemo";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
string FileName="G:\\My_Documents\\核材料管理软件\\AcceptData\\12345678.dbf";
string name = null;
FileInfo file = new FileInfo(FileName);
name = file.Name;//文件名
string path = file.Directory.FullName + "\\"; //文件目录
string strConnection = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=227;Dbq=" + path;
string strSelect="SELECT * FROM " + name;
OdbcConnection thisConnection = new OdbcConnection(strConnection);
thisConnection.Open();
OdbcDataAdapter thisAdapter = new OdbcDataAdapter(strSelect,thisConnection);
// OdbcCommandBuilder thisBuilder = new OdbcCommandBuilder(thisAdapter);
DataSet thisDataSet = new DataSet();
// name = name.Substring(0,name.Length - 4);
try
{
thisAdapter.Fill(thisDataSet,name);//如果name的长度超过8位会有错误
}
catch(Exception exec)
{
MessageBox.Show(exec.Message);
return;
}
int n = thisDataSet.Tables[name].Rows.Count;
this.dataGrid1.DataSource = thisDataSet.Tables[name];
MessageBox.Show(n.ToString ());
}
}
}
Top
17 楼ldy(罗大佑)回复于 2003-12-09 09:30:06 得分 0
我改变了驱动:
string strConnection = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + path;
这样就不存在问题了。gxz01 你试一下哈
====================================================
葬我于高山之上兮,望我大陆。大陆不可见兮,只有痛哭。
葬我于高山之上兮,望我故乡。故乡不可见兮,永远不忘。
天苍苍,野茫茫,山之上,国有殇。
====================================================Top
18 楼gxz01()回复于 2003-12-09 12:13:38 得分 0
多谢ldy(罗大佑)
我现在试一下.Top
19 楼gxz01()回复于 2003-12-09 12:35:13 得分 0
To 罗大佑
我改了驱动,还是有问题,现在连8位的文件名也打不开了,麻烦你帮我看一下
我的问题在哪里,谢谢
string FileName="c:\\12345678.dbf";
string name = null;
FileInfo file = new FileInfo(FileName);
name = file.Name;//文件名
//name=name.Substring(0,name.Length-4);
string path = file.Directory.FullName + "\\"; //文件目录
string strConnection = "Driver={Microsoft Visual FoxProDriver};SourceType=DBF;SourceDB=" + path;
//string strConnection = "Driver={Microsoft dBASE Driver (*.dbf)};DriverID=227;Dbq=" + path;
string strSelect="SELECT * FROM " + name;
OdbcConnection thisConnection = new OdbcConnection(strConnection);
thisConnection.Open();
OdbcDataAdapter thisAdapter = new OdbcDataAdapter(strSelect,thisConnection);//这里就出错
DataSet thisDataSet = new DataSet();
try
{
thisAdapter.Fill(thisDataSet,name);
}
catch(Exception exec)
{
MessageBox.Show(exec.Message);
return;
}
int n = thisDataSet.Tables[name].Rows.Count;
MessageBox.Show(n.ToString ());
Top
20 楼ldy(罗大佑)回复于 2003-12-09 15:01:56 得分 0
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.Odbc;
using System.IO;
namespace Sample
{
/// <summary>
/// frmDBFDemo 的摘要说明。
/// </summary>
public class frmDBFDemo : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button btnRetrieve;
private System.Windows.Forms.Button btnCancel;
private System.Data.Odbc.OdbcConnection odbcConnection1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmDBFDemo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnRetrieve = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.odbcConnection1 = new System.Data.Odbc.OdbcConnection();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(384, 280);
this.dataGrid1.TabIndex = 0;
//
// btnRetrieve
//
this.btnRetrieve.Location = new System.Drawing.Point(392, 224);
this.btnRetrieve.Name = "btnRetrieve";
this.btnRetrieve.TabIndex = 1;
this.btnRetrieve.Text = "&Retrieve";
this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(392, 256);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "&Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// frmDBFDemo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(480, 285);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnRetrieve);
this.Controls.Add(this.dataGrid1);
this.Name = "frmDBFDemo";
this.Text = "frmDBFDemo";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
string FileName = "G:\\VSPRJ\\FuelMIS\\AcceptData\\022003050001-3.dbf";
string name = null;
FileInfo file = new FileInfo(FileName);
name = file.Name;//文件名
string path = file.Directory.FullName + "\\"; //文件目录
string strConnection = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + path;
string strSelect="SELECT * FROM " + name;
OdbcConnection thisConnection = new OdbcConnection(strConnection);
thisConnection.Open();
OdbcDataAdapter thisAdapter = new OdbcDataAdapter(strSelect,thisConnection);
DataSet thisDataSet = new DataSet();
try
{
thisAdapter.Fill(thisDataSet,name);
}
catch(Exception exec)
{
MessageBox.Show(exec.Message);
return;
}
this.dataGrid1.DataSource = thisDataSet.Tables[name];
}
}
}
Top
21 楼ldy(罗大佑)回复于 2003-12-09 15:03:00 得分 0
我的没问题啊
我的环境是DotNet 1.1
====================================================
小时候
乡愁是一枚小小的邮票
我在这头
母亲在那头
长大后乡愁是一张窄窄的船票
我在这头
新娘在那头
后来呵
乡愁是一方矮矮的坟墓
我在外头
母亲呵在里头
而现在
乡愁是一湾浅浅的海峡
我在这头
大陆在那头
Top
22 楼gxz01()回复于 2003-12-09 16:56:19 得分 0
我的好向是1.0.3705,
不知上面这个是不是版本号,
你说的DotNet 1.1哪里有下载的Top
23 楼ldy(罗大佑)回复于 2003-12-09 18:52:08 得分 0
如果是框架的话,MS官方就有得下
如果是环境,那么用GOOGLE搜索一下Top
24 楼gxz01()回复于 2004-03-08 15:02:18 得分 0
To ldy(罗大佑)
using System.Data.Odbc;
请问一下,你上边这一句是怎么来的,我的System.Data里没有Odbc,只有OleDb;
我用的是
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Data.Odbc;
using System.IO;
Microsoft.Data.Odbc.dll是从Microsoft那里下载的.
System.Data.Odbc应该重哪里找,谢谢.
Top
25 楼ldy(罗大佑)回复于 2004-03-08 22:14:58 得分 0
Version 1.1.4322Top




