欢迎光临
我们一直在努力

将DBF,XLS,XML,MDB文件导入C#DataGrid的方法-.NET教程,C#语言

建站超值云服务器,限时71元/月

以下的源码里分别给出了将dbf,xls,xml,mdb文件导入c#datagrid的方法,供各位参考。

//putindataset.cs的源码

using system;

using system.data.odbc;

using system.data.oledb;

using system.data;

using system.collections;

namespace putindataset

{

/// <summary>

/// datasettransin 的摘要说明。

/// </summary>

public class putindataset

{

/// <summary>

/// 传入的文件变量

/// </summary>

private dataset my_ds;//存放文件的数据集

private string my_err;//错误信息

private string my_tablename;//传入的文件名

private tabletype my_tabletype;//传入的文件类型

private string my_tablepath;//传入的文件路径

private int my_tableindex;//表的索引

oledbcommandbuilder my_builder;//命令串

/// <summary>

/// 数据库连接变量

/// </summary>

private string my_strconnection;//连接字符串

private string my_strselect;//select语句

/// <summary>

/// 可以处理的文件类型

/// </summary>

public enum tabletype

{

mdb,xls,dbf,doc,txt,xml,html

}

public putindataset(string tablepath,string tablename,tabletype tabletype)

{

///<summary>

///获得传入的路径,文件名及文件类型;

///</summary>

this.my_tablepath=tablepath;//路径

this.my_tablename=tablename;//文件名

this.my_tabletype=tabletype;//文件类型

}

public dataset convert()

{

dataset irtn_ds=new dataset();

switch (this.my_tabletype)

{

case tabletype.dbf:

irtn_ds = this.dbftods();

break;

case tabletype.mdb:

irtn_ds = this.mdbtods();

break;

case tabletype.xls:

irtn_ds = this.xlstods();

break;

case tabletype.xml:

irtn_ds = this.xmltods();

break;

}

return irtn_ds;

}

///<summary>

///将dbf文件放入dataset

///</summary>

private dataset dbftods()

{

//数据库连接定义

odbcconnection my_conn; //数据连接

odbcdataadapter my_adapter;//数据适配器

//数据库连接

this.my_strconnection= "driver={microsoft visual foxpro driver};sourcetype=dbf;sourcedb=" + this.my_tablepath;

this.my_strselect="select * from " + this.my_tablename;

my_conn = new odbcconnection(this.my_strconnection);

my_conn.open();

my_adapter = new odbcdataadapter(this.my_strselect,my_conn);

this.my_ds=new dataset();

//填充数据集

my_adapter.fill(this.my_ds,this.my_tablename);

return this.my_ds;

}

///<summary>

///将mdb文件放入dataset

///</summary>

private dataset mdbtods()

{

//数据库连接定义

oledbconnection my_conn;

oledbdataadapter my_adapter;

//数据库连接

this.my_strconnection= "provider=microsoft.jet.oledb.4.0;data source=" + this.my_tablepath;

this.my_strselect="select * from " + this.my_tablename;

my_conn = new oledbconnection(this.my_strconnection);

my_conn.open();

my_adapter = new oledbdataadapter(this.my_strselect,my_conn);

this.my_ds=new dataset();

//填充数据集

my_adapter.fill(this.my_ds,this.my_tablename);

return this.my_ds;

}

///<summary>

///将xml文件放入dataset

///</summary>

private dataset xmltods()

{

//填充数据集

this.my_ds=new dataset();

this.my_ds.readxml(this.my_tablepath+this.my_tablename,xmlreadmode.readschema);

this.my_ds.datasetname="xmldata";

return this.my_ds;

}

///<summary>

///将excel文件放入dataset

///</summary>

private dataset xlstods()

{

oledbconnection my_conn;

oledbdataadapter my_adapter;

//数据库连接

this.my_strconnection= "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source="+this.my_tablepath+this.my_tablename;

this.my_strselect="select * from [sheet1$]";

my_conn = new oledbconnection(this.my_strconnection);

my_conn.open();

my_adapter = new oledbdataadapter(this.my_strselect,my_conn);

this.my_builder=new oledbcommandbuilder(my_adapter);

this.my_ds=new dataset();

//填充数据集

my_adapter.fill(this.my_ds,"exceldata");

return this.my_ds;

}

}

}

//form_putindataset.cs的源码

using system;

using system.data;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using dataaccess.sysmanage;

using businessrules;

using datasettrans;

namespace winform.common

{

/// <summary>

/// formdesktop 的摘要说明。

/// </summary>

public class formdesktop : system.windows.forms.form

{

private winform.common.desktop desktop1;

private system.windows.forms.button button1;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;

private dataset m_ds = new dataset();

private system.windows.forms.datagrid datagrid1; //数据源

private string m_tablename; //外部文件名称

public formdesktop()

{

//

// windows 窗体设计器支持所必需的

//

initializecomponent();

//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

#region windows form designer generated code

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.desktop1 = new winform.common.desktop();

this.button1 = new system.windows.forms.button();

this.datagrid1 = new system.windows.forms.datagrid();

((system.componentmodel.isupportinitialize)(this.datagrid1)).begininit();

this.suspendlayout();

//

// desktop1

//

this.desktop1.backcolor = system.drawing.systemcolors.desktop;

this.desktop1.location = new system.drawing.point(168, 440);

this.desktop1.name = "desktop1";

this.desktop1.size = new system.drawing.size(16, 24);

this.desktop1.tabindex = 0;

//

// button1

//

this.button1.location = new system.drawing.point(256, 216);

this.button1.name = "button1";

this.button1.size = new system.drawing.size(88, 40);

this.button1.tabindex = 1;

this.button1.text = "getdata";

this.button1.click += new system.eventhandler(this.button1_click);

//

// datagrid1

//

this.datagrid1.datamember = "";

this.datagrid1.headerforecolor = system.drawing.systemcolors.controltext;

this.datagrid1.location = new system.drawing.point(192, 40);

this.datagrid1.name = "datagrid1";

this.datagrid1.size = new system.drawing.size(216, 152);

this.datagrid1.tabindex = 2;

//

// formdesktop

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.backcolor = system.drawing.systemcolors.appworkspace;

this.clientsize = new system.drawing.size(624, 485);

this.controls.add(this.datagrid1);

this.controls.add(this.button1);

this.controls.add(this.desktop1);

this.name = "formdesktop";

this.text = "系统控制台";

this.resize += new system.eventhandler(this.formdesktop_resize);

this.load += new system.eventhandler(this.formdesktop_load);

((system.componentmodel.isupportinitialize)(this.datagrid1)).endinit();

this.resumelayout(false);

}

#endregion

private void formdesktop_load(object sender, system.eventargs e)

{

}

/// <summary>

/// 当窗口改变大小时自动居中。

/// </summary>

private void formdesktop_resize(object sender, system.eventargs e)

{

if (this.windowstate != formwindowstate.minimized)

{

if (this.width > this.desktop1.width && this.height > this.desktop1.height )

{

this.desktop1.left = (this.width – this.desktop1.width) / 2;

this.desktop1.top = (this.height- this.desktop1.height)/ 2;

}

}

}

private void button1_click(object sender, system.eventargs e)

{

dataset out_ds=new dataset();

putindataset obj=new putindataset("文件路径","文件名",putindataset.tabletype.文件格式);//调用putindataset类

out_ds=obj.convert();//转换到dataset中

this.datagrid1.datasource=out_ds.tables["表名"];//在datagrid中显示

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 将DBF,XLS,XML,MDB文件导入C#DataGrid的方法-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章