Ways to import dbf,xls,xml,mdb files into C#datagrid

Source: Internet
Author: User
Tags definition table name
Datagrid|xml The following source code is given in the Dbf,xls,xml,mdb file into the C#datagrid method, for you to reference.

PutInDataSet.cs's source code
Using System;
Using System.Data.Odbc;
Using System.Data.OleDb;
Using System.Data;
Using System.Collections;

Namespace Putindataset
{
<summary>
Summary description of the Datasettransin.
</summary>
public class Putindataset
{
<summary>
File variables passed in
</summary>
Private dataset my_ds;//data set for storing files
private string my_err;//error message
private string my_tablename;//The file name passed in
Private tabletype my_tabletype;//Incoming file type
private string my_tablepath;//incoming file path
Index of private int my_tableindex;//table
OleDbCommandBuilder my_builder;//Command string

<summary>
Database connection variables
</summary>
private string my_strconnection;//Connection string
private string My_strselect;//select statement

<summary>
Types of files that can be processed
</summary>
public enum Tabletype
{
Mdb,xls,dbf,doc,txt,xml,html
}

Public Putindataset (String tablepath,string tablename,tabletype tabletype)
{
<summary>
Get the incoming path, filename and file type;
</summary>
this.my_tablepath=tablepath;//Path
this.my_tablename=tablename;//file name
this.my_tabletype=tabletype;//file type
}

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>
Putting DBF files into a dataset
</summary>
Private DataSet Dbftods ()
{
Database connection Definition
OdbcConnection My_conn; Data connection
OdbcDataAdapter my_adapter;//Data Adapter

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

Populating data sets
My_adapter.fill (This.my_ds,this.my_tablename);
return this.my_ds;
}

<summary>
Putting an MDB file into a dataset
</summary>
Private DataSet Mdbtods ()
{
Database connection Definition
OleDbConnection My_conn;
OleDbDataAdapter my_adapter;

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

Populating data sets
My_adapter.fill (This.my_ds,this.my_tablename);
return this.my_ds;
}

<summary>
Putting an XML file into a dataset
</summary>
Private DataSet Xmltods ()
{

Populating data sets
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>
Put an Excel file into a dataset
</summary>
Private DataSet Xlstods ()
{
OleDbConnection My_conn;
OleDbDataAdapter my_adapter;

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

Populating data sets
My_adapter.fill (This.my_ds, "exceldata");
return this.my_ds;
}


}
}


Form_putindataset.cs's source code

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>
Summary description of the formdesktop.
</summary>
public class FormDesktop:System.Windows.Forms.Form
{
Private WinForm.Common.DeskTop DeskTop1;
Private System.Windows.Forms.Button button1;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Private DataSet M_ds = new DataSet ();
Private System.Windows.Forms.DataGrid dataGrid1; Data source
private string M_tablename; External file name

Public Formdesktop ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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 = "System console";
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>
Automatically center when the window changes to a large hour.
</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 ("File path", "filename", putindataset.tabletype. file format);//Call Putindataset class
Out_ds=obj. convert ();//Conversion to DataSet
this.datagrid1.datasource=out_ds.tables["Table name"];//displayed in the DataGrid

}
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.