C# 实现DB文件的导入导出功能

1.     DB文件的导出

private void InsertDB(DataTable dt, string FileName)
        {
            try
            {
                string userIds = string.Empty;
                string[] str = ccbUserName.Text.Split(',');
                for (int s = 0; s < str.Length; s++)
                {
                    userIds += GetUserId(str[s].Trim()) + "',";
                }
                userIds = userIds.Substring(0, userIds.Length - 1);
                if (string.IsNullOrEmpty(userIds))
                {return;
                }

                SqliteBaseClass sqlite = new SqliteBaseClass(FileName);
                try
                {
                    sqlite.creatDataBase();//创建数据库
                    sqlite.SqliteConn = new System.Data.SQLite.SQLiteConnection(sqlite.StrConntion);
                    sqlite.SqliteConn.Open();
                    //创建表
                    sqlite.creatDataBaseTable("CREATE TABLE GpsInfo ([id] CHAR(32),[gpsTime] DATETIME,[Lon] DOUBLE,[Lat] DOUBLE,[speed] DOUBLE,[direction] DOUBLE,[seaLevel] DOUBLE,[uploaded] INT,[TotalLiCheng] nvarchar(50))");
                    sqlite.creatDataBaseTable("CREATE TABLE SysParamters ([param] NVARCHAR(50) NOT NULL,[pValue] NVARCHAR(150))");
                    sqlite.Strtablename = "SysParamters";
                    //插入gps数据
                    sqlite.Strtablename = "GpsInfo";
                    string fields = "id,gpsTime,lon,lat,speed,direction,sealevel,TotalLiCheng";//字段列表
                    string values = string.Empty;//数据列表
                    uiProgressBar1.Maximum = dt.Rows.Count;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //把数组数据转换为,分割
                        string[] strs = dt.Rows[i].ItemArray.Select(w => w.ToString()).ToArray<string>();
                        //时间类型存入db时要转换一下,不然读取时报错
                        DateTime date = Convert.ToDateTime(dt.Rows[i]["时间"]);
                        values = "'" + strs[0] + "','" + date.ToString("s") + "','" + string.Join("','", strs, 3, 6) + "'";
                        sqlite.insertFieldData(fields, values);//插入数据
                        uiProgressBar1.Value = i;
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (sqlite!=null&&sqlite.SqliteConn!=null)
                    {
                        sqlite.SqliteConn.Close();
                    }
                }
                MsgBoxShow.ShowInformation("导出成功!");
            }
            catch (Exception ex)
            {
                MsgBoxShow.ShowWarning("导出错误:" + ex.Message);
            }
        }

 

2.     DB文件的导入

 
  // 获取DB数据

        private void GetDBData(object sender, WaitWindowEventArgs e)
        {
            try
            {
                if (e.Arguments.Count > 0)
                {
                    //创建sqlite操作
                    SqliteBaseClass sqlite = new SqliteBaseClass(e.Arguments[0].ToString());
                    sqlite.SqliteConn = new System.Data.SQLite.SQLiteConnection(sqlite.StrConntion);
                    sqlite.SqliteConn.Open();
                    //查询格式是否正确
                    string strgps = "SELECT id,gpsTime,Lon,Lat,speed,direction,TotalLiCheng,seaLevel FROM GpsInfo Limit 1";
                    string strsys = "SELECT param,pValue FROM SysParamters";
                    DataSet gpsSet = sqlite.getDataSetQueryContSql(strgps);
                    DataSet SysSet = sqlite.getDataSetQueryContSql(strsys);
                    if (gpsSet == null || SysSet == null || gpsSet.Tables[0].Rows.Count <= 0)
                    {
                        MsgBoxShow.ShowInformation("DB文件格式错误或者不存在数据");
                        return;
                    }

                    //获取数据
                    string strGpsInfo = "SELECT id as 巡护人员,gpsTime as 时间,Lon as 经度,Lat as 纬度,speed as 速度,direction as 方向,"
                        + " TotalLiCheng as 总里程,seaLevel as 海拔 FROM GpsInfo  ORDER BY gpsTime";
                    DataTable GpsInfoDT = sqlite.getDataSetQueryContSql(strGpsInfo).Tables[0];

                    //计算总里程
                    e.Result = CalculateTotalLiCheng(GpsInfoDT);
                }
            }
            catch (Exception ex)
            {
                MsgBoxShow.ShowWarning("打开DB文件错误:" + ex.Message);
            }
        }
    原文作者:sqlite
    原文地址: https://www.cnblogs.com/lailewei/p/4072963.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞