将Sqlite数据写入Sqlserver
/// <summary> /// 上传本地信息至服务器 /// </summary> public static void UploadDataToServer() { try { #region 1.读取本地数据 StringBuilder sbSelect = new StringBuilder(); foreach (string tblName in CMMConstants.UPLOADTABLENAMES) { // 查询未上传的数据 sbSelect.Append(string.Format(" SELECT * FROM {0} WHERE IsUpload='0' OR IsUpload IS NULL;", tblName)); } SQLiteDBManager _SQLiteDBManager = new SQLiteDBManager(); DataSet ds = _SQLiteDBManager.GetDataSet(sbSelect.ToString(), null); for (int i = 0; i < CMMConstants.UPLOADTABLENAMES.Length; i++) { // 设置DataTableName ds.Tables[i].TableName = CMMConstants.UPLOADTABLENAMES[i]; } #endregion #region 2.上传至服务器 // 服务器数据访问 DBManager _DBManager = new DBManager(); // 获取服务器表的字段列表 DataSet dsColumns = _DBManager.GetColumns(CMMConstants.UPLOADTABLENAMES.ToList()); List<SqlBulkCopyColumnMapping> mappingCollection = null; DataTable dtTemp = null; DataTable dtData = null; string sColumnName = null; // 循环多张表 foreach (string tblName in CMMConstants.UPLOADTABLENAMES) { dtData = ds.Tables[tblName]; dtTemp = dsColumns.Tables[tblName]; mappingCollection = new List<SqlBulkCopyColumnMapping>(); if (dtData.Rows.Count > 0) { StringBuilder sbSql = new StringBuilder(); List<SqlParameter> lstPara = new List<SqlParameter>(); string paraName = null; string sKey = CMMConstants.UPLOADTABLENAMES_PrimaryKey[tblName]; for (int rowIndex = 0; rowIndex < dtData.Rows.Count; rowIndex++) { // 循环数据行 StringBuilder sbInsertSql = new StringBuilder(); StringBuilder sbInsertPara = new StringBuilder(); StringBuilder sbUpdateSql = new StringBuilder(); for (int i = 0; i < dtTemp.Rows.Count; i++) { // 循环列名集合 sColumnName = dtTemp.Rows[i]["name"].ToString(); if (dtData.Columns.Contains(sColumnName)) { sbInsertSql.Append(sColumnName); paraName = string.Format("@{0}{1}", sColumnName, rowIndex); sbInsertPara.Append(paraName); lstPara.Add(new SqlParameter(paraName, dtData.Rows[rowIndex][sColumnName])); if (sKey != sColumnName) { // 过滤掉主键 sbUpdateSql.Append(string.Format("{0}={1}", sColumnName, paraName)); } if (i != dtTemp.Rows.Count - 1) { sbInsertSql.Append(","); sbInsertPara.Append(","); if (sKey != sColumnName) { sbUpdateSql.Append(","); } } } } sbSql.Append(string.Format(" if not exists(select 1 from {0} where {1}=@{1}{2}) begin insert into {0}({3}) values({4}) end else begin update {0} set {5} where {1}=@{1}{2} end ", tblName, sKey, rowIndex, sbInsertSql, sbInsertPara, sbUpdateSql)); // lstPara.Add(new SqlParameter(string.Format("@{0}{1}", sKey, rowIndex), dtData.Rows[rowIndex][sKey])); } _DBManager.BeginTran(); int iResult = _DBManager.ExeSql(sbSql.ToString(), lstPara.ToArray()); _DBManager.CommitTran(); if (iResult > 0) { // 更新本地数据上传状态 string sKeyName = CMMConstants.UPLOADTABLENAMES_PrimaryKey[tblName]; string sInStr = InStr(dtData, sKeyName); string sUpdateSql = string.Format("UPDATE {0} SET IsUpload='{1}' where {2} IN({3})", tblName, "1", sKeyName, sInStr); iResult = _SQLiteDBManager.ExeSql(sUpdateSql); } } } #endregion } catch (Exception ex) { throw ex; } }
PS:
1、 本使用sqlbulkcopy来向服务器写数据,无奈数据类型转换异常,只得改用拼sql来向服务器写入数据 。
2、 CMMConstants.UPLOADTABLENAMES_PrimaryKey 为字典类型的键值对 表名:主键列名 。
3、 CMMConstants.UPLOADTABLENAMES 为数组类型的表名列表 。