C# 实现DB文件的导入导出功能
生活随笔
收集整理的這篇文章主要介紹了
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);}}轉載于:https://www.cnblogs.com/lailewei/p/4072963.html
總結
以上是生活随笔為你收集整理的C# 实现DB文件的导入导出功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于高性能的那点事
- 下一篇: hadoop中汉字与英文字符混合的key