All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
ASP.NET技巧:存储过程的分析_[Asp.Net教程]
SQL数据库高级教程:SQL ALTER TABLE_[SQL Server教程]
技巧:使用User,Control做HTML生成_[Asp.Net教程]
将Web页转换为ASP.NET 2.0用户控件
Asp.Net,网站项目打包_[Asp.Net教程]
ASP.Net中MD5和SHA1加密的几种方法_[Asp.Net教程]
ASP.NET2.0数据库入门之SqlDataSource_[Asp.Net教程]
Sqlserver应用内置工具建立审查系统_[SQL,Server教程]
彻底放弃IIS让Apache也支持ASP.NET_.net资料_编程技术
ASP.NET,2.0后台代码与内联代码的对比_[Asp.Net教程]
Reading number is top 10 pictures
50个至今影响世界的德国发明
南昌铁路局宜春车务段攸县车站铁路职工福利房被开发商侵占
漂亮的跳舞妹妹1
战场废物2
Small QiShu -- ShuangShuangPan1
明星与豪宅
小学生作文又现神作,你不得不佩服
怀春少女-石一伊
The terra-cotta warriors2
水晶头骨造型的酒瓶
Download software ranking
jBuilder2006
株洲本地在线棋牌游戏
Kung.Fu.Panda.2
SP4 for SQL2000
C语言教程TXT
仙剑奇侠传98硬盘WINXP版
WebService在.NET中的实战应用教学视频 → 第4集
Red cliff
Unix video tutorial20
传奇私服架设教程-chm
delv published in(发表于) 2014/1/16 9:33:08 Edit(编辑)
将DBF,XLS,XML,MDB文件导入C#DataGrid的方法_[Asp.Net教程]_0

将DBF,XLS,XML,MDB文件导入C#DataGrid的方法_[Asp.Net教程]_0

将DBF,XLS,XML,MDB文件导入C#DataGrid的方法_[Asp.Net教程]

以下的源码里分别给出了将DBF,XLS,XML,MDB文件导入C#DataGrid的方法,供各位参考。
复制C#代码保存代码//PutInDataSet.cs的源码
using System;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data;
using System.Collections;


namespace PutInDataSet
{
///


/// DataSetTransIn 的摘要说明。
///

public class PutInDataSet
{
///
/// 传入的文件变量
///

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;//命令串


///


/// 数据库连接变量
///

private string my_StrConnection;//连接字符串
private string my_StrSelect;//select语句


///


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

public enum TableType
{
MDB,
XLS,
DBF,
DOC,
TXT,
XML,
HTML
}


public PutInDataSet(string TablePath, string TableName, TableType TableType)
{
///


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

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;
}


///


///将DBF文件放入DataSet
///

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;
}


///


///将MDB文件放入DataSet
///

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;
}


///


///将XML文件放入DataSet
///

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;
}


///


///将Excel文件放入DataSet
///

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;
}
}
}
复制C#代码保存代码//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
{
///


/// FormDesktop 的摘要说明。
///

public class FormDesktop : System.Windows.Forms.Form
{
private WinForm.Common.DeskTop deskTop1;
private System.Windows.Forms.Button button1;
///
/// 必需的设计器变量。
///

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();


//
// TOD 在 InitializeComponent 调用后添加任何构造函数代码
//
}


///


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

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}


#region Windows Form Designer generated code
///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

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)
{


}


///


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

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中显示


}
}
}


来源:阿良.NET







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.