/// <summary> /// 获取SQl脚本 /// </summary> /// <param name="row"></param> /// <param name="keys"></param> /// <returns></returns> public static string GetSQLScript(MDataRow row, params string[] keys) { DalType dalType = CrossDb.GetDalType(row.TableName); string tableName = DBTool.Keyword(row.TableName, dalType); string where = string.Empty; StringBuilder sb = new StringBuilder(); //sb.AppendFormat("if not EXISTS (select 1 from {0} where ", tableName); //foreach (string key in keys) //{ // if (where != string.Empty) // { // where += " and "; // } // where +=DBTool.Keyword(key,dalType)+"='" + row[key].ToString() + "'"; //} //sb.Append(where); //sb.AppendLine(")\r\nbegin");//Insert sb.AppendFormat("insert into {0}(", tableName);//Insert string columns = string.Empty; foreach (var ct in row.Columns) { columns += DBTool.Keyword(ct.ColumnName, dalType) + ","; } sb.Append(columns.TrimEnd(',') + ") \r\n values("); columns = string.Empty; foreach (var ct in row.Columns) { if (row[ct.ColumnName].IsNull) { columns += "null,"; } else { columns += "'" + row[ct.ColumnName].ToString() + "',"; } } sb.Append(columns.TrimEnd(',') + ")\r\n"); //sb.AppendLine("end"); //sb.AppendLine("else"); //sb.AppendLine("begin");//Update //sb.AppendFormat("update {0} set ", tableName);//Insert //columns = string.Empty; //List<string> items = new List<string>(); //items.AddRange(keys); //foreach (var ct in row.Columns) //{ // if (items.Contains(ct.ColumnName)) // { // continue; // } // if (row[ct.ColumnName].IsNull) // { // columns += DBTool.Keyword(ct.ColumnName,dalType)+ "=null,"; // } // else // { // columns += DBTool.Keyword(ct.ColumnName,dalType) + "='" + row[ct.ColumnName].ToString() + "',"; // } //} //sb.AppendFormat(columns.TrimEnd(',') + " where {0}\r\n", where); //sb.AppendLine("end \r\n"); return(sb.ToString()); }
/// <summary> /// 数据结构行刷新 /// </summary> /// <param name="objName"></param> /// <param name="objCode"></param> /// <param name="dt">原有数据</param> /// <returns></returns> public static bool Flesh(string objName, string objCode, MDataTable dt, out string msg) { bool result = false; msg = LangConst.NoNewColumn; MDataTable newDt = dt.GetSchema(false); //移除表结构缓存 string tableKey = CacheManage.GetKey(CacheKeyType.Schema, objName, CrossDb.GetDBName(objName), CrossDb.GetDalType(objName)); CacheManage.LocalInstance.Remove(tableKey); FillTable(objName, objCode, newDt);//重新获取。 MDataTable addTable = dt.GetSchema(false); // bool needUpdate = false; foreach (MDataRow row in newDt.Rows) { MDataRow mr = dt.FindRow(string.Format("Field='{0}'", row.Get <string>("Field"))); if (mr == null)//找不到,则添加行 { row.Set(Config_Grid.OrderNum, 255); row.Set(Config_Grid.Hidden, true); row.Set(Config_Grid.GridID, Guid.NewGuid()); addTable.Rows.Add(row); } } if (addTable.Rows.Count > 0) { result = addTable.AcceptChanges(AcceptOp.InsertWithID); if (!result) { msg = Convert.ToString((Exception)addTable.DynamicData); } } return(result); }