【转】c# DBF数据库导入导出实例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;

namespace DbfExample
{
    public partial class Form1 : Form
    {
        System.Data.Odbc.OdbcConnection conn;
        public Form1()
        {
            InitializeComponent();
        }
        //导出数据
        private void btnOut_Click(object sender, EventArgs e)
        {
            string connect = "Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:\\; ";
            OdbcConnection myconn = new OdbcConnection(connect);
            string sqlt ="CREATE TABLE aa.DBF (cc int(10))";
            
            myconn.Open();
            OdbcCommand olec = new OdbcCommand(sqlt, myconn);
            try
            {
                int i = olec.ExecuteNonQuery(); 
                MessageBox.Show("'" + i + "'success");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                olec.Dispose();
                myconn.Close();
            }
            
            //string ole_connstring = @"Provider=microsoft.jet.oledb.5.0;Data Source=D:\;";
            //System.Data.OleDb.OleDbConnection ole_conn = new System.Data.OleDb.OleDbConnection(ole_connstring);
            //try
            //{
            //    ole_conn.Open();
            //    System.Data.OleDb.OleDbCommand cmd1 = new System.Data.OleDb.OleDbCommand
            //             ("Create Table TestTable (Field1 int, Field2 char(10),Field float(10,2))",
            //             ole_conn);
            //    System.Data.OleDb.OleDbCommand cmd2 = new System.Data.OleDb.OleDbCommand
            //             ("Insert Into TestTable values (1,'Hello3',520.20)", ole_conn);
            //    System.Data.OleDb.OleDbCommand cmd3 = new System.Data.OleDb.OleDbCommand
            //             ("Insert Into TestTable values (2,'Hello4',18076.60)", ole_conn);
            //    cmd1.ExecuteNonQuery();
            //    cmd2.ExecuteNonQuery();
            //    cmd3.ExecuteNonQuery();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
            //finally
            //{
            //    ole_conn.Close();
            //}

        }
        //导入数据
        private void btnIn_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bind();
        }
        private void Bind()
        {
            try
            {
                conn = new System.Data.Odbc.OdbcConnection();
                string table = @"C:\测试例子\Dbf\prepur.dbf";
                string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";

                conn.ConnectionString = connStr;
                conn.Open();

                string sql = @"select * from " + table;

                OdbcDataAdapter da = new OdbcDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                this.dataGridView1.DataSource = dt.DefaultView;
                //MessageBox.Show(dt.Rows[0][0].ToString());
            }
            catch
            { }
            finally
            {
                conn.Close();
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                conn = new System.Data.Odbc.OdbcConnection();
                string table = @"C:\测试例子\Dbf\table1.dbf";
                string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";

                conn.ConnectionString = connStr;
                conn.Open();
                string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                string sql = @"select * from " + table + " where id='" + id + "'";
                OdbcDataAdapter da = new OdbcDataAdapter(sql, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                txtId.Text = id;
                txtName.Text = dt.Rows[0]["name"].ToString();
                txtAddress.Text = dt.Rows[0]["address"].ToString();
            }   
            catch { }
            finally
            {
                conn.Close();
            }
        }
        private void Add()
        {

            conn = new System.Data.Odbc.OdbcConnection();
            string table = @"C:\temp\Dbf\table1.dbf";
            string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + table + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";

            conn.ConnectionString = connStr;
            conn.Open();
            OdbcCommand cmd = new OdbcCommand();
            cmd.Connection = conn;
            string sql = "insert into " + table + " values('" + txtId.Text + "','" + txtName.Text + "','" + txtAddress.Text + "')";
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            Bind();
        }

        private void btnTOSQL_Click(object sender, EventArgs e)
        {
            try
            {
                string sql = "Insert Into dbftosql select * From openrowset('MSDASQL','Driver=Microsoft Visual FoxPro Driver;SourceType=DBF;SourceDB=C:\\temp\\Dbf','select * from table1.dbf')";
                SqlConnection con = new SqlConnection("server=.;database=labelprint;uid=sa;pwd=sa");
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

posted @ 2013-06-27 11:49  从良少年  阅读(1075)  评论(0编辑  收藏  举报