博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《WinForm 系列》- DBF文件导入导出
阅读量:6533 次
发布时间:2019-06-24

本文共 4494 字,大约阅读时间需要 14 分钟。

hot3.png

项目结构

104437_qeDh_2330610.png

设置属性

private const string DbfTAbleName = "dbf_user";private string _saveFileName = string.Empty;

导出DBF

初始化数据

#region 初始化数据 - InitData()private List
InitData(){ List
list = new List
(); for (int i = 0; i < 100; i++) { User user = new User() { ID = i, Name = "Jack" + i }; list.Add(user); } return list;}#endregion

保存路径

#region 保存文件 - SaveData()private void SaveData(){    SaveFileDialog saveFile = new SaveFileDialog();    saveFile.Filter = "所有DBF文件|*.DBF";//设置文件类型    saveFile.Title = "导出DBF数据";//设置标题     saveFile.AddExtension = true;    saveFile.FileName = DbfTAbleName + ".DBF";    saveFile.AutoUpgradeEnabled = true;    if (saveFile.ShowDialog() == DialogResult.OK)    {        _saveFileName = saveFile.FileName;    }}#endregion

开始导出(核心方法)

#region 导出文件 - ExportData()private void ExportData(){    #region 1.0 创建OLEDB连接    string basePath = string.Format(@"{0}", AppDomain.CurrentDomain.BaseDirectory);    string mStrConn = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + basePath + @"/;Extended Properties=""dBASE IV;HDR=Yes;"";";    string filePath = string.Format(@"{0}\{1}.DBF", basePath, DbfTAbleName);    if (File.Exists(filePath) == true)    {        File.Delete(filePath);    }    #endregion    #region 2.0 创建DBF表文件    #region 2.1 创建表    using (OleDbConnection connection = new OleDbConnection(mStrConn))    {        connection.Open();        string sqlt = string.Format("create table {0} (ID varchar(20),Name varchar(20))", DbfTAbleName);        OleDbCommand OLDBComm = new OleDbCommand(sqlt, connection);        OLDBComm.ExecuteNonQuery();        OLDBComm.Dispose();    }    #endregion    #region 2.2 删除表    using (OleDbConnection connection = new OleDbConnection(mStrConn))    {        connection.Open();        OleDbCommand OLDBCommIn = new OleDbCommand(string.Format("delete * from {0}", DbfTAbleName), connection);        OLDBCommIn.ExecuteNonQuery();        OLDBCommIn.Dispose();    }    #endregion    #endregion 创建DBF表文件    #region 3.0 导出数据    #region 3.1 查询数据            List
userList = InitData(); #endregion #region 3.2 生成数据 using (OleDbConnection connection = new OleDbConnection(mStrConn)) { connection.Open(); OleDbCommand OLDBCommInsert = new OleDbCommand(); foreach (User user in userList) { string sql_add = string.Format("insert into {0} (ID,Name) values({1},'{2}')", DbfTAbleName, user.ID, user.Name); OLDBCommInsert.CommandText = sql_add; OLDBCommInsert.CommandType = CommandType.Text; OLDBCommInsert.Connection = connection; OLDBCommInsert.ExecuteNonQuery(); } File.Copy(filePath, _saveFileName, true); //copy file MessageBox.Show("导出数据成功!"); } #endregion #endregion}#endregion

导出按钮

private void btnExport_Click(object sender, EventArgs e){    SaveData();    ExportData();}

导入DBF

获取DBF文件

#region 获取上传的dbf路径 - void GetDbf(out string dbfDir, out string dbfName)private void GetDbf(out string dbfDir, out string dbfName){    OpenFileDialog ofd = new OpenFileDialog();    ofd.InitialDirectory = "@D:\\";    ofd.Filter = "dbf文件|*.dbf";    string filePath = string.Empty;    if (ofd.ShowDialog() == DialogResult.OK)    {        filePath = ofd.FileName;        dbfDir = Path.GetDirectoryName(filePath);        dbfName = Path.GetFileNameWithoutExtension(filePath);    }    else    {        dbfDir = string.Empty;        dbfName = string.Empty;    }}#endregion

上传数据

#region 上传数据 -  void ImportData(string dbfDir,string dbfName)private void ImportData(string dbfDir, string dbfName){    OdbcConnection conn = new OdbcConnection();    conn.ConnectionString = "Driver={Microsoft dBase Driver (*.dbf)};DefaultDir=" + dbfDir;    conn.Open();    string strQuery = "select * from " + dbfName;    OdbcDataAdapter da = new OdbcDataAdapter(strQuery, conn.ConnectionString);    DataTable table = new DataTable();    da.Fill(table);    dgv.DataSource = table;}#endregion

导入按钮

private void btnImport_Click(object sender, EventArgs e){    string dbfDir = "";    string dbfName = "";    GetDbf(out dbfDir, out dbfName);    if (MessageBox.Show("导入数据", "请确认信息", MessageBoxButtons.YesNo) == DialogResult.Yes)    {        ImportData(dbfDir, dbfName);    }}

 

转载于:https://my.oschina.net/kimisme/blog/1608256

你可能感兴趣的文章
Spring Boot整合MyBatis
查看>>
网址导航类的网站为什么会没落
查看>>
windows无法创建快捷方式 请检查磁盘已满
查看>>
jquery处理textarea中的手动换行
查看>>
使用DELPHI编写一个小的控件
查看>>
利用v$enqueue_lock解决ORA-14450的错误
查看>>
Windows系统磁盘分区详解
查看>>
MySQLdb的安装与使用
查看>>
032 文本框中的时间格式
查看>>
Operations Manager 2007 R2系列之邮件通知
查看>>
Windows数据类型探幽——千回百转你是谁?(1)
查看>>
参加虚拟化达人训练营的体会
查看>>
当DiscuzNT遇上了Loadrunner(中)
查看>>
Entity Framework练习题
查看>>
快速排序法——较优方法
查看>>
linuxQQ
查看>>
[转]三维成像原理
查看>>
android116 轮播 viewPager实现
查看>>
mongodb压缩——snappy、zlib块压缩,btree索引前缀压缩
查看>>
Oracle查看和修改其最大的游标数
查看>>