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 設計工具產生的程式碼#region Windows Form 設計工具產生的程式碼 /**//// <summary> /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改 /// 這個方法的內容。 /// </summary> private void InitializeComponent() ...{ this.groupBox1 = new System.Windows.Forms.GroupBox(); this.btnDelete = new System.Windows.Forms.Button(); this.btnInsert = new System.Windows.Forms.Button(); this.btnCopy = new System.Windows.Forms.Button(); this.btnCreate = new System.Windows.Forms.Button(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.btnOpen = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.btnDelete); this.groupBox1.Controls.Add(this.btnInsert); this.groupBox1.Controls.Add(this.btnCopy); this.groupBox1.Controls.Add(this.btnCreate); this.groupBox1.Controls.Add(this.dataGrid1); this.groupBox1.Controls.Add(this.btnOpen); this.groupBox1.Controls.Add(this.btnExit); this.groupBox1.Controls.Add(this.btnSave); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill; this.groupBox1.Location = new System.Drawing.Point(0, 0); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(520, 325); this.groupBox1.TabIndex = 44; this.groupBox1.TabStop = false; // // btnDelete // this.btnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnDelete.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnDelete.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnDelete.Location = new System.Drawing.Point(232, 280); this.btnDelete.Name = "btnDelete"; this.btnDelete.Size = new System.Drawing.Size(64, 32); this.btnDelete.TabIndex = 48; this.btnDelete.Text = "&Delete"; this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click); // // btnInsert // this.btnInsert.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnInsert.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnInsert.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnInsert.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnInsert.Location = new System.Drawing.Point(160, 280); this.btnInsert.Name = "btnInsert"; this.btnInsert.Size = new System.Drawing.Size(64, 32); this.btnInsert.TabIndex = 47; this.btnInsert.Text = "&Insert"; this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click); // // btnCopy // this.btnCopy.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnCopy.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnCopy.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnCopy.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnCopy.Location = new System.Drawing.Point(368, 280); this.btnCopy.Name = "btnCopy"; this.btnCopy.Size = new System.Drawing.Size(64, 32); this.btnCopy.TabIndex = 46; this.btnCopy.Text = "&Copy"; this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click); // // btnCreate // this.btnCreate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnCreate.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnCreate.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnCreate.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnCreate.Location = new System.Drawing.Point(16, 280); this.btnCreate.Name = "btnCreate"; this.btnCreate.Size = new System.Drawing.Size(64, 32); this.btnCreate.TabIndex = 45; this.btnCreate.Text = "&Create"; this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(3, 18); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(514, 246); this.dataGrid1.TabIndex = 44; // // btnOpen // this.btnOpen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnOpen.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnOpen.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnOpen.Location = new System.Drawing.Point(88, 280); this.btnOpen.Name = "btnOpen"; this.btnOpen.Size = new System.Drawing.Size(64, 32); this.btnOpen.TabIndex = 0; this.btnOpen.Text = "&Open"; this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click); // // btnExit // this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnExit.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnExit.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnExit.Location = new System.Drawing.Point(440, 280); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(64, 32); this.btnExit.TabIndex = 43; this.btnExit.Text = "E&xit"; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // btnSave // this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btnSave.Font = new System.Drawing.Font("SimSun", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.btnSave.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.btnSave.Location = new System.Drawing.Point(296, 280); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(64, 32); this.btnSave.TabIndex = 1; this.btnSave.Text = "&Save"; this.btnSave.Click += new System.EventHandler(this.btnSave_Click); // // HR_Form_PersonnelExportDBF // this.AutoScaleBaseSize = new System.Drawing.Size(5, 15); this.ClientSize = new System.Drawing.Size(520, 325); this.Controls.Add(this.groupBox1); this.Name = "HR_Form_PersonnelExportDBF"; this.Text = "C#操作DBF"; this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion [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) ...{ 選擇存放文件的路徑,並複製表結構到此路徑#region 選擇存放文件的路徑,並複製表結構到此路徑 this.Cursor=Cursors.WaitCursor; if(this.folderBrowserDialog1.ShowDialog()==DialogResult.OK) ...{ if(folderBrowserDialog1.SelectedPath.Trim().Substring(folderBrowserDialog1.SelectedPath.Trim().Length)!="/") folderBrowserDialog1.SelectedPath=folderBrowserDialog1.SelectedPath.Trim()+"/"; this._FilePath=folderBrowserDialog1.SelectedPath; string sourceFile_Dbf=Application.StartupPath+"/"+this._FileName+".dbf"; string sourceFile_Cdx=Application.StartupPath+"/"+this._FileName+".cdx"; string destFile_Dbf=this._FilePath+this._FileName+".dbf"; string destFile_Cdx=this._FilePath+this._FileName+".cdx"; if(System.IO.File.Exists(sourceFile_Dbf) && System.IO.File.Exists(sourceFile_Cdx)) ...{ System.IO.File.Copy(sourceFile_Dbf,destFile_Dbf,true); System.IO.File.Copy(sourceFile_Cdx,destFile_Cdx,true); } else throw new Exception("結構文件不存在或不完整!"); } this.Cursor=Cursors.Default; #endregion } }}
有兩個問題:
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
其他資訊: 系統錯誤。