天天看点

C#中用Odbc操作DBF

C#中用Odbc操作DBF

using  System;

C#中用Odbc操作DBF

using  System.Drawing;

C#中用Odbc操作DBF

using  System.Collections;

C#中用Odbc操作DBF

using  System.ComponentModel;

C#中用Odbc操作DBF

using  System.Windows.Forms;

C#中用Odbc操作DBF

using  System.Data;

C#中用Odbc操作DBF

using  System.Data.Odbc;

C#中用Odbc操作DBF

using  System.Threading;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

namespace  test

C#中用Odbc操作DBF
C#中用Odbc操作DBF

... {

C#中用Odbc操作DBF
C#中用Odbc操作DBF

    /// <summary>

C#中用Odbc操作DBF

    /// HR_Form_PersonnelExportDBF 的摘要描述。

C#中用Odbc操作DBF

    /// </summary>

C#中用Odbc操作DBF

    public class HR_Form_PersonnelExportDBF : System.Windows.Forms.Form  

C#中用Odbc操作DBF
C#中用Odbc操作DBF

    ...{

C#中用Odbc操作DBF

        private System.Windows.Forms.GroupBox groupBox1;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnOpen;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnExit;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnSave;

C#中用Odbc操作DBF

        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;

C#中用Odbc操作DBF

        System.Windows.Forms.BindingManagerBase myBind;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 文件名(不包含扩展名)

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private string _FileName="r_da";

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 文件路径

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private string _FilePath="";

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 连接字符串

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private string _myStrConnection;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// select语句

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private string _myStrSelect;

C#中用Odbc操作DBF

        OdbcDataAdapter _myAdapter;

C#中用Odbc操作DBF

        OdbcConnection _myconn;

C#中用Odbc操作DBF

        DataSet myDataSet=new DataSet();

C#中用Odbc操作DBF

        private System.Windows.Forms.DataGrid dataGrid1;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnCreate;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnCopy;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnInsert;

C#中用Odbc操作DBF

        private System.Windows.Forms.Button btnDelete;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 設計工具所需的變數。

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private System.ComponentModel.Container components = null;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        public HR_Form_PersonnelExportDBF()

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            InitializeComponent();

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF
C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 清除任何使用中的資源。

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        protected override void Dispose( bool disposing )

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            if( disposing )

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            ...{

C#中用Odbc操作DBF

                if(components != null)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

                ...{

C#中用Odbc操作DBF

                    components.Dispose();

C#中用Odbc操作DBF

                }

C#中用Odbc操作DBF

            }

C#中用Odbc操作DBF

            base.Dispose( disposing );

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF
C#中用Odbc操作DBF
C#中用Odbc操作DBF

        Windows Form 設計工具產生的程式碼#region Windows Form 設計工具產生的程式碼

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        /// <summary>

C#中用Odbc操作DBF

        /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改

C#中用Odbc操作DBF

        /// 這個方法的內容。

C#中用Odbc操作DBF

        /// </summary>

C#中用Odbc操作DBF

        private void InitializeComponent()

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            this.groupBox1 = new System.Windows.Forms.GroupBox();

C#中用Odbc操作DBF

            this.btnDelete = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.btnInsert = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.btnCopy = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.btnCreate = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.dataGrid1 = new System.Windows.Forms.DataGrid();

C#中用Odbc操作DBF

            this.btnOpen = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.btnExit = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.btnSave = new System.Windows.Forms.Button();

C#中用Odbc操作DBF

            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();

C#中用Odbc操作DBF

            this.groupBox1.SuspendLayout();

C#中用Odbc操作DBF

            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

C#中用Odbc操作DBF

            this.SuspendLayout();

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // groupBox1

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnDelete);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnInsert);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnCopy);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnCreate);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.dataGrid1);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnOpen);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnExit);

C#中用Odbc操作DBF

            this.groupBox1.Controls.Add(this.btnSave);

C#中用Odbc操作DBF

            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;

C#中用Odbc操作DBF

            this.groupBox1.Location = new System.Drawing.Point(0, 0);

C#中用Odbc操作DBF

            this.groupBox1.Name = "groupBox1";

C#中用Odbc操作DBF

            this.groupBox1.Size = new System.Drawing.Size(520, 325);

C#中用Odbc操作DBF

            this.groupBox1.TabIndex = 44;

C#中用Odbc操作DBF

            this.groupBox1.TabStop = false;

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnDelete

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnDelete.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnDelete.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnDelete.Location = new System.Drawing.Point(232, 280);

C#中用Odbc操作DBF

            this.btnDelete.Name = "btnDelete";

C#中用Odbc操作DBF

            this.btnDelete.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnDelete.TabIndex = 48;

C#中用Odbc操作DBF

            this.btnDelete.Text = "&Delete";

C#中用Odbc操作DBF

            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnInsert

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnInsert.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnInsert.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnInsert.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnInsert.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnInsert.Location = new System.Drawing.Point(160, 280);

C#中用Odbc操作DBF

            this.btnInsert.Name = "btnInsert";

C#中用Odbc操作DBF

            this.btnInsert.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnInsert.TabIndex = 47;

C#中用Odbc操作DBF

            this.btnInsert.Text = "&Insert";

C#中用Odbc操作DBF

            this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnCopy

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnCopy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnCopy.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnCopy.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnCopy.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnCopy.Location = new System.Drawing.Point(368, 280);

C#中用Odbc操作DBF

            this.btnCopy.Name = "btnCopy";

C#中用Odbc操作DBF

            this.btnCopy.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnCopy.TabIndex = 46;

C#中用Odbc操作DBF

            this.btnCopy.Text = "&Copy";

C#中用Odbc操作DBF

            this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnCreate

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnCreate.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnCreate.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnCreate.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnCreate.Location = new System.Drawing.Point(16, 280);

C#中用Odbc操作DBF

            this.btnCreate.Name = "btnCreate";

C#中用Odbc操作DBF

            this.btnCreate.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnCreate.TabIndex = 45;

C#中用Odbc操作DBF

            this.btnCreate.Text = "&Create";

C#中用Odbc操作DBF

            this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // dataGrid1

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.dataGrid1.DataMember = "";

C#中用Odbc操作DBF

            this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;

C#中用Odbc操作DBF

            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;

C#中用Odbc操作DBF

            this.dataGrid1.Location = new System.Drawing.Point(3, 18);

C#中用Odbc操作DBF

            this.dataGrid1.Name = "dataGrid1";

C#中用Odbc操作DBF

            this.dataGrid1.Size = new System.Drawing.Size(514, 246);

C#中用Odbc操作DBF

            this.dataGrid1.TabIndex = 44;

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnOpen

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnOpen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnOpen.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnOpen.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnOpen.Location = new System.Drawing.Point(88, 280);

C#中用Odbc操作DBF

            this.btnOpen.Name = "btnOpen";

C#中用Odbc操作DBF

            this.btnOpen.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnOpen.TabIndex = 0;

C#中用Odbc操作DBF

            this.btnOpen.Text = "&Open";

C#中用Odbc操作DBF

            this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnExit

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;

C#中用Odbc操作DBF

            this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnExit.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnExit.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnExit.Location = new System.Drawing.Point(440, 280);

C#中用Odbc操作DBF

            this.btnExit.Name = "btnExit";

C#中用Odbc操作DBF

            this.btnExit.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnExit.TabIndex = 43;

C#中用Odbc操作DBF

            this.btnExit.Text = "E&xit";

C#中用Odbc操作DBF

            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // btnSave

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));

C#中用Odbc操作DBF

            this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;

C#中用Odbc操作DBF

            this.btnSave.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

C#中用Odbc操作DBF

            this.btnSave.ImeMode = System.Windows.Forms.ImeMode.NoControl;

C#中用Odbc操作DBF

            this.btnSave.Location = new System.Drawing.Point(296, 280);

C#中用Odbc操作DBF

            this.btnSave.Name = "btnSave";

C#中用Odbc操作DBF

            this.btnSave.Size = new System.Drawing.Size(64, 32);

C#中用Odbc操作DBF

            this.btnSave.TabIndex = 1;

C#中用Odbc操作DBF

            this.btnSave.Text = "&Save";

C#中用Odbc操作DBF

            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            // HR_Form_PersonnelExportDBF

C#中用Odbc操作DBF

            // 

C#中用Odbc操作DBF

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);

C#中用Odbc操作DBF

            this.ClientSize = new System.Drawing.Size(520, 325);

C#中用Odbc操作DBF

            this.Controls.Add(this.groupBox1);

C#中用Odbc操作DBF

            this.Name = "HR_Form_PersonnelExportDBF";

C#中用Odbc操作DBF

            this.Text = "C#操作DBF";

C#中用Odbc操作DBF

            this.groupBox1.ResumeLayout(false);

C#中用Odbc操作DBF

            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();

C#中用Odbc操作DBF

            this.ResumeLayout(false);

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        #endregion

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        [STAThread] 

C#中用Odbc操作DBF

      static void Main() 

C#中用Odbc操作DBF
C#中用Odbc操作DBF

      ...{ 

C#中用Odbc操作DBF

            Application.Run(new HR_Form_PersonnelExportDBF()); 

C#中用Odbc操作DBF

      }

C#中用Odbc操作DBF

        private void btnExit_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            this.Close();

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnCreate_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            //数据库连接

C#中用Odbc操作DBF

            this._myStrConnection=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:;Exclusive=Yes;";

C#中用Odbc操作DBF

            using(_myconn = new OdbcConnection(this._myStrConnection))

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            ...{

C#中用Odbc操作DBF

                _myconn.Open();

C#中用Odbc操作DBF

                //生成的dbf文件存放在應用程序所在目錄下,而不是指定的目錄(C:)下,需要手工去複製到指定的目錄

C#中用Odbc操作DBF

                this._FileName="r_da";

C#中用Odbc操作DBF

                using(OdbcCommand comm=new OdbcCommand("create table "+this._FileName+"(da_gh c(7) UNIQUE,da_xm c(8)",_myconn))

C#中用Odbc操作DBF
C#中用Odbc操作DBF

                ...{

C#中用Odbc操作DBF

                    int a=comm.ExecuteNonQuery();//返回值為-1

C#中用Odbc操作DBF

                }

C#中用Odbc操作DBF

            }

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnOpen_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            //数据库连接

C#中用Odbc操作DBF

            this._myStrConnection=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB="+Application.StartupPath+";Exclusive=Yes;";

C#中用Odbc操作DBF

            _myconn = new OdbcConnection(this._myStrConnection);

C#中用Odbc操作DBF

            _myconn.Open();

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            this._myStrSelect="SELECT da_gh,da_xm FROM " + this._FileName;

C#中用Odbc操作DBF

            this._myAdapter = new OdbcDataAdapter(this._myStrSelect,_myconn);

C#中用Odbc操作DBF

//            OdbcCommandBuilder commandbuilder = new OdbcCommandBuilder(_myAdapter);

C#中用Odbc操作DBF

            this.setDataAdapter(this._myAdapter);

C#中用Odbc操作DBF

            //填充数据集

C#中用Odbc操作DBF

            _myAdapter.Fill(this.myDataSet,this._FileName);

C#中用Odbc操作DBF

            this.dataGrid1.DataSource=this.myDataSet.Tables[this._FileName].DefaultView;

C#中用Odbc操作DBF

            this.myBind=this.BindingContext[this.dataGrid1.DataSource];

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void setDataAdapter(OdbcDataAdapter dataAdapter)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            string field;

C#中用Odbc操作DBF

            OdbcParameter para;

C#中用Odbc操作DBF

            //參數的設定參考了以下文章

C#中用Odbc操作DBF

            //http://msdn.microsoft.com/library/cht/default.asp?url=/library/CHT/cpguide/html/cpconupdatingdatabasewithdataadapterdataset.asp

C#中用Odbc操作DBF

            //http://msdn.microsoft.com/library/cht/default.asp?url=/library/CHT/cpguide/html/cpconusingparameterswithdataadapters.asp

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            //insertCommand

C#中用Odbc操作DBF

            dataAdapter.InsertCommand=new OdbcCommand("insert into "+this._FileName+"(da_gh,da_xm) values(?,?)",this._myconn);

C#中用Odbc操作DBF

            field="da_gh";

C#中用Odbc操作DBF

            para=dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));

C#中用Odbc操作DBF

            para.SourceColumn=field;

C#中用Odbc操作DBF

            field="da_xm";

C#中用Odbc操作DBF

            para=dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));

C#中用Odbc操作DBF

            para.SourceColumn=field;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            //updateCommand

C#中用Odbc操作DBF

            dataAdapter.UpdateCommand=new OdbcCommand("update "+this._FileName+" set da_xm=? where da_gh=?",this._myconn);

C#中用Odbc操作DBF

            field="da_gh";

C#中用Odbc操作DBF

            para=dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));

C#中用Odbc操作DBF

            para.SourceColumn=field;

C#中用Odbc操作DBF

            para.SourceVersion=DataRowVersion.Original;

C#中用Odbc操作DBF

            field="da_xm";

C#中用Odbc操作DBF

            para=dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));

C#中用Odbc操作DBF

            para.SourceColumn=field;

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            //deleteCommand

C#中用Odbc操作DBF

            dataAdapter.DeleteCommand=new OdbcCommand("delete from "+this._FileName+" where da_gh=?",this._myconn);

C#中用Odbc操作DBF

            field="da_gh";

C#中用Odbc操作DBF

            para=dataAdapter.DeleteCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));

C#中用Odbc操作DBF

            para.SourceColumn=field;

C#中用Odbc操作DBF

            para.SourceVersion=DataRowVersion.Original;

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnInsert_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            if(this.myBind!=null)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            ...{

C#中用Odbc操作DBF

                int row=this.myDataSet.Tables[this._FileName].Rows.Count;

C#中用Odbc操作DBF

                DataRow dr=this.myDataSet.Tables[this._FileName].NewRow();

C#中用Odbc操作DBF

                dr["da_gh"]="b3"+row.ToString().PadLeft(5,'0');

C#中用Odbc操作DBF

                dr["da_xm"]="張三"+row.ToString().PadLeft(4,'0');

C#中用Odbc操作DBF

                this.myDataSet.Tables[this._FileName].Rows.Add(dr);

C#中用Odbc操作DBF

            }

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnDelete_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            if(this.myBind!=null && this.myBind.Count>0)

C#中用Odbc操作DBF

                (this.myBind.Current as DataRowView).Delete();

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnSave_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF

            if(_myconn.State==ConnectionState.Closed)

C#中用Odbc操作DBF

                _myconn.Open();

C#中用Odbc操作DBF

            this._myAdapter.Update(this.myDataSet,this._FileName);

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

        private void btnCopy_Click(object sender, System.EventArgs e)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

        ...{

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            選擇存放文件的路徑,並複製表結構到此路徑#region 選擇存放文件的路徑,並複製表結構到此路徑

C#中用Odbc操作DBF

            this.Cursor=Cursors.WaitCursor;

C#中用Odbc操作DBF

            if(this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)

C#中用Odbc操作DBF
C#中用Odbc操作DBF

            ...{

C#中用Odbc操作DBF

                if(folderBrowserDialog1.SelectedPath.Trim().Substring(folderBrowserDialog1.SelectedPath.Trim().Length)!="/")

C#中用Odbc操作DBF

                    folderBrowserDialog1.SelectedPath=folderBrowserDialog1.SelectedPath.Trim()+"/";

C#中用Odbc操作DBF

                this._FilePath=folderBrowserDialog1.SelectedPath;

C#中用Odbc操作DBF

                string sourceFile_Dbf=Application.StartupPath+"/"+this._FileName+".dbf";

C#中用Odbc操作DBF

                string sourceFile_Cdx=Application.StartupPath+"/"+this._FileName+".cdx";

C#中用Odbc操作DBF

                string destFile_Dbf=this._FilePath+this._FileName+".dbf";

C#中用Odbc操作DBF

                string destFile_Cdx=this._FilePath+this._FileName+".cdx";

C#中用Odbc操作DBF

                if(System.IO.File.Exists(sourceFile_Dbf) && System.IO.File.Exists(sourceFile_Cdx))

C#中用Odbc操作DBF
C#中用Odbc操作DBF

                ...{

C#中用Odbc操作DBF

                    System.IO.File.Copy(sourceFile_Dbf,destFile_Dbf,true);

C#中用Odbc操作DBF

                    System.IO.File.Copy(sourceFile_Cdx,destFile_Cdx,true);

C#中用Odbc操作DBF

                }    

C#中用Odbc操作DBF

                else

C#中用Odbc操作DBF

                    throw new Exception("結構文件不存在或不完整!");

C#中用Odbc操作DBF

            }

C#中用Odbc操作DBF

            this.Cursor=Cursors.Default;

C#中用Odbc操作DBF

            #endregion

C#中用Odbc操作DBF

        }

C#中用Odbc操作DBF

    }

C#中用Odbc操作DBF

}

C#中用Odbc操作DBF

有兩個問題:

1.創建dbf(btnCreate_Click事件)時錯誤:

類型 'System.Data.Odbc.OdbcException' 的未處理例外狀況發生於 system.data.dll

其他資訊: 系統錯誤。

2.在讀取已經存在的dbf到DataSet中后,刪除記錄后,按"Save"按鈕保存數據ok;但當修改了記錄或插入新記錄后,保存數據時出錯誤:

1.創建dbf(btnCreate_Click事件)時錯誤:

類型 'System.Data.Odbc.OdbcException' 的未處理例外狀況發生於 system.data.dll

其他資訊: 系統錯誤。