C#中用Odbc操作DBF

    技术2022-05-11  11

    using  System; using  System.Drawing; using  System.Collections; using  System.ComponentModel; using  System.Windows.Forms; using  System.Data; using  System.Data.Odbc; using  System.Threading; namespace  test {    /// <summary>    /// HR_Form_PersonnelExportDBF 的摘要描述。    /// </summary>    public class HR_Form_PersonnelExportDBF : System.Windows.Forms.Form      {        private System.Windows.Forms.GroupBox groupBox1;        private System.Windows.Forms.Button btnOpen;        private System.Windows.Forms.Button btnExit;        private System.Windows.Forms.Button btnSave;        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;        System.Windows.Forms.BindingManagerBase myBind;        /// <summary>        /// 文件名(不包含扩展名)        /// </summary>        private string _FileName="r_da";        /// <summary>        /// 文件路径        /// </summary>        private string _FilePath="";        /// <summary>        /// 连接字符串        /// </summary>        private string _myStrConnection;        /// <summary>        /// select语句        /// </summary>        private string _myStrSelect;        OdbcDataAdapter _myAdapter;        OdbcConnection _myconn;        DataSet myDataSet=new DataSet();        private System.Windows.Forms.DataGrid dataGrid1;        private System.Windows.Forms.Button btnCreate;        private System.Windows.Forms.Button btnCopy;        private System.Windows.Forms.Button btnInsert;        private System.Windows.Forms.Button btnDelete;        /// <summary>        /// 設計工具所需的變數。        /// </summary>        private System.ComponentModel.Container components = null;        public HR_Form_PersonnelExportDBF()        {            InitializeComponent();        }        /// <summary>        /// 清除任何使用中的資源。        /// </summary>        protected override void Dispose( bool disposing )        {            if( disposing )            {                if(components != null)                {                    components.Dispose();                }            }            base.Dispose( disposing );        }        Windows Form 設計工具產生的程式碼        [STAThread]       static void Main()       {             Application.Run(new HR_Form_PersonnelExportDBF());       }        private void btnExit_Click(object sender, System.EventArgs e)        {            this.Close();        }        private void btnCreate_Click(object sender, System.EventArgs e)        {            //数据库连接            this._myStrConnection=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=c:;Exclusive=Yes;";            using(_myconn = new OdbcConnection(this._myStrConnection))            {                _myconn.Open();                //生成的dbf文件存放在應用程序所在目錄下,而不是指定的目錄(C:)下,需要手工去複製到指定的目錄                this._FileName="r_da";                using(OdbcCommand comm=new OdbcCommand("create table "+this._FileName+"(da_gh c(7) UNIQUE,da_xm c(8)",_myconn))                {                    int a=comm.ExecuteNonQuery();//返回值為-1                }            }        }        private void btnOpen_Click(object sender, System.EventArgs e)        {            //数据库连接            this._myStrConnection=@"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB="+Application.StartupPath+";Exclusive=Yes;";            _myconn = new OdbcConnection(this._myStrConnection);            _myconn.Open();            this._myStrSelect="SELECT da_gh,da_xm FROM " + this._FileName;            this._myAdapter = new OdbcDataAdapter(this._myStrSelect,_myconn);//            OdbcCommandBuilder commandbuilder = new OdbcCommandBuilder(_myAdapter);            this.setDataAdapter(this._myAdapter);            //填充数据集            _myAdapter.Fill(this.myDataSet,this._FileName);            this.dataGrid1.DataSource=this.myDataSet.Tables[this._FileName].DefaultView;            this.myBind=this.BindingContext[this.dataGrid1.DataSource];        }        private void setDataAdapter(OdbcDataAdapter dataAdapter)        {            string field;            OdbcParameter para;            //參數的設定參考了以下文章            //http://msdn.microsoft.com/library/cht/default.asp?url=/library/CHT/cpguide/html/cpconupdatingdatabasewithdataadapterdataset.asp            //http://msdn.microsoft.com/library/cht/default.asp?url=/library/CHT/cpguide/html/cpconusingparameterswithdataadapters.asp            //insertCommand            dataAdapter.InsertCommand=new OdbcCommand("insert into "+this._FileName+"(da_gh,da_xm) values(?,?)",this._myconn);            field="da_gh";            para=dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));            para.SourceColumn=field;            field="da_xm";            para=dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));            para.SourceColumn=field;            //updateCommand            dataAdapter.UpdateCommand=new OdbcCommand("update "+this._FileName+" set da_xm=? where da_gh=?",this._myconn);            field="da_gh";            para=dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));            para.SourceColumn=field;            para.SourceVersion=DataRowVersion.Original;            field="da_xm";            para=dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));            para.SourceColumn=field;            //deleteCommand            dataAdapter.DeleteCommand=new OdbcCommand("delete from "+this._FileName+" where da_gh=?",this._myconn);            field="da_gh";            para=dataAdapter.DeleteCommand.Parameters.Add(new OdbcParameter("@"+field,System.Data.Odbc.OdbcType.NVarChar));            para.SourceColumn=field;            para.SourceVersion=DataRowVersion.Original;        }        private void btnInsert_Click(object sender, System.EventArgs e)        {            if(this.myBind!=null)            {                int row=this.myDataSet.Tables[this._FileName].Rows.Count;                DataRow dr=this.myDataSet.Tables[this._FileName].NewRow();                dr["da_gh"]="b3"+row.ToString().PadLeft(5,'0');                dr["da_xm"]="張三"+row.ToString().PadLeft(4,'0');                this.myDataSet.Tables[this._FileName].Rows.Add(dr);            }        }        private void btnDelete_Click(object sender, System.EventArgs e)        {            if(this.myBind!=null && this.myBind.Count>0)                (this.myBind.Current as DataRowView).Delete();        }        private void btnSave_Click(object sender, System.EventArgs e)        {            if(_myconn.State==ConnectionState.Closed)                _myconn.Open();            this._myAdapter.Update(this.myDataSet,this._FileName);        }        private void btnCopy_Click(object sender, System.EventArgs e)        {            選擇存放文件的路徑,並複製表結構到此路徑        }    }}

    有兩個問題:

    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

    其他資訊: 系統錯誤。


    最新回复(0)