zoukankan      html  css  js  c++  java
  • 【转】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:测试例子Dbfprepur.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 able1.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: empDbf able1.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);
                }
            }
        }
    }

  • 相关阅读:
    关于医学的一点想法
    我的ArcGis9.3 到Arcgis10.0 升级步骤
    最近一月的娱乐生活:看电影,玩游戏
    最近一月的娱乐生活:看电影,玩游戏
    5年技术学习历程的回顾
    5年技术学习历程的回顾
    网站开发的技术选型问题
    网站开发的技术选型问题
    学技术真累
    Java实现 LeetCode 200 岛屿数量
  • 原文地址:https://www.cnblogs.com/congliang/p/DBFExport.html
Copyright © 2011-2022 走看看