public string GetMultiSelect(int pageTemplateId, int columnDefId) { try { var pageTemplate = SessionService.PageTemplate(pageTemplateId); var columnDef = SessionService.ColumnDef(columnDefId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities Db = new TargetEntities()) { Db.Database.Connection.ConnectionString = dbEntity.ConnectionString; var columnValue = columnDef.ColumnName; var columnText = columnDef.ColumnName; var tableName = pageTemplate.TableName; var sql = "SELECT DISTINCT ISNULL(CAST(" + columnValue + " AS varchar(500)),'') AS ValueField, ISNULL(CAST(" + columnText + " AS varchar(500)),'') AS TextField FROM " + tableName + " ORDER BY TextField"; if (columnDef.ElementType == "DropdownCustomOption") { sql = "SELECT OptionValue AS ValueField, OptionText AS TextField FROM CustomOption WHERE ColumnDefId = " + columnDefId + " ORDER BY TextField"; } else if (columnDef.ElementType == "DropdownSimple") { sql = "SELECT ISNULL(CAST(" + columnDef.ValueField + " AS varchar(250)),'') AS ValueField, ISNULL(CAST(" + columnDef.TextField.Replace(",", " + ' - ' + ") + " AS varchar(500)),'') AS TextField FROM " + columnDef.LookupTable + " ORDER BY TextField"; } var recs = Db.Database.SqlQuery <ValueText>(sql).ToList(); var json = JsonConvert.SerializeObject(recs); return(json); } } catch (Exception ex) { return(ex.Message); } }
public string GetCustomOption(int customDefId, int dbEntityId) { var dbEntity = SessionService.DbEntity(dbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; var recs = targetDb.Database.SqlQuery <CustomOption>("SELECT * FROM CustomOption WHERE ColumnDefId = " + customDefId); string json = JsonConvert.SerializeObject(recs); return(json); } }
public string EncryptRecords(int columnDefId) { var numOfRecord = 0; var columnName = ""; try { StringBuilder sb = new StringBuilder(); var i = 0; var columnDef = SessionService.ColumnDef(columnDefId); var pageTemplate = SessionService.PageTemplate(columnDef.PageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; var encryptedValue = ""; columnName = columnDef.ColumnName; var tableName = pageTemplate.TableName; var primaryKey = pageTemplate.PrimaryKey; var primaryKeyType = pageTemplate.PrimaryKeyType; var sql = "SELECT CAST(" + primaryKey + " AS nvarchar(36)) AS RecordId, CAST(ISNULL(" + columnName + ", '') AS varchar(max)) AS RecordValue FROM " + tableName; var exe = "UPDATE " + tableName + " SET " + columnName + " = '[RecordValue]' WHERE " + primaryKey + " = '[RecordId]';"; var items = targetDb.Database.SqlQuery <EncrypedRecord>(sql).ToList(); foreach (var item in items.Where(w => w.RecordValue.Length > 0)) { encryptedValue = Crypto.Encrypt(item.RecordValue).Replace("'", "''"); sb.Append(exe.Replace("[RecordId]", item.RecordId).Replace("[RecordValue]", encryptedValue)); numOfRecord++; i++; if (i > 50) { i = 0; targetDb.Database.ExecuteSqlCommand(sb.ToString()); sb.Clear(); } } if (i > 0 && sb.Length > 0) { targetDb.Database.ExecuteSqlCommand(sb.ToString()); } } return("Successful. " + numOfRecord + " records were encrypted for column " + columnName + "."); } catch (Exception ex) { return(ex.Message); } }
public string DeleteChildTableRecord(int pageTemplateId, int recordId) { PageTemplate pageTemplate = SessionService.PageTemplate(pageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; string sql = "DELETE FROM " + pageTemplate.TableName + " WHERE " + pageTemplate.PrimaryKey + " = " + recordId; targetDb.Database.ExecuteSqlCommand(sql); return("T"); } }
public static string GetPrimaryKey(int pageTemplateId, string tableName) { var pageTemplate = SessionService.PageTemplate(pageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities Db = new TargetEntities()) { Db.Database.Connection.ConnectionString = dbEntity.ConnectionString; var columnName = Db.Database.SqlQuery <string>("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY(OBJECT_ID(CONSTRAINT_SCHEMA + '.' + CONSTRAINT_NAME), 'IsPrimaryKey') = 1 AND TABLE_NAME = '" + tableName + "'").FirstOrDefault(); if (columnName != null) { return(columnName); } } return(""); }
public string DeleteRecord(string ids_, int pageTemplateId) { try { var pageTemplate = SessionService.PageTemplate(pageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; var exe = "DELETE FROM " + pageTemplate.TableName + " WHERE " + pageTemplate.PrimaryKey + " IN (" + ids_ + ")"; targetDb.Database.ExecuteSqlCommand(exe); targetDb.SaveChanges(); } return(""); } catch (Exception ex) { return(ex.Message); } }
public string GetCustomOptions(int pageTemplateId, int columnDefId) { string json = ""; PageTemplate pageTemplate = SessionService.PageTemplate(pageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; List <ValueText> recs = targetDb.Database.SqlQuery <ValueText>("SELECT OptionValue AS ValueField, OptionText AS TextField FROM CustomOption WHERE ColumnDefId = " + columnDefId + " ORDER BY OptionText").ToList(); json = Newtonsoft.Json.JsonConvert.SerializeObject(recs); if (json != null && json.Length > 2) { json = "[{ \"ValueField\":\"\", \"TextField\":\"\"}," + json.Substring(1, json.Length - 1); } } return(json); }
public string GetSortColumnOptionsByName(string tableName, int dbEntityId) { StringBuilder sb = new StringBuilder(); var json = ""; sb.Append("SELECT c.name + ' ASC' AS ValueField, c.name + ' Ascending' AS TextField FROM sys.columns c JOIN sys.objects o ON o.object_id = c.object_id WHERE o.type = 'U' AND o.name = '" + tableName + "' "); sb.Append("UNION "); sb.Append("SELECT c.name + ' DESC' AS ValueField, c.name + ' Descending' AS TextField FROM sys.columns c JOIN sys.objects o ON o.object_id = c.object_id WHERE o.type = 'U' AND o.name = '" + tableName + "' "); sb.Append("ORDER BY ValueField"); var dbEntity = SessionService.DbEntity(dbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; var recs = targetDb.Database.SqlQuery <ValueText>(sb.ToString()); json = Newtonsoft.Json.JsonConvert.SerializeObject(recs); json = "[{ \"ValueField\":\"\", \"TextField\":\"\"}," + json.Substring(1, json.Length - 1); return(json); } }
public string GetOptionsByColumnDefId(int pageTemplateId, int columnDefId) { var columnDef = SessionService.ColumnDefs(pageTemplateId).Where(w => w.ColumnDefId == columnDefId).FirstOrDefault(); string orderBy = ""; if (columnDef.OrderField.Length > 3) { orderBy = " ORDER BY " + columnDef.OrderField; } string textField = ""; if (columnDef.TextField.Contains(",")) { var columns = columnDef.TextField.Split(new char[] { ',' }); foreach (var columnName in columns) { var dataType = SessionService.DataType(pageTemplateId, columnName); if (dataType == "TEXT") { if (textField.Length == 0) { textField = columnName; } else { textField = textField + " + ' - ' + " + columnName; } } else { if (textField.Length == 0) { textField = "CAST(" + columnName + " AS varchar)"; } else { textField = textField + " + ' - ' + " + "CAST(" + columnName + " AS varchar)"; } } } } else { textField = columnDef.TextField; } string sql = "SELECT CAST(" + columnDef.ValueField + " AS varchar) AS ValueField, (" + textField + ") AS TextField FROM " + columnDef.LookupTable + " " + orderBy; string json = ""; PageTemplate pageTemplate = SessionService.PageTemplate(pageTemplateId); var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; var recs = targetDb.Database.SqlQuery <ValueText>(sql); json = Newtonsoft.Json.JsonConvert.SerializeObject(recs); if (json.Length < 2) { json = "[];"; } } if (columnDef.AddBlankOption) { json = "[{ \"ValueField\":\"\", \"TextField\":\"\"}," + json.Substring(1, json.Length - 1); } return(json); }
public string GetFormData(int pageTemplateId = 0, string recordId = "", string layoutType = "") { try { string tableName = ""; string primaryKey = ""; string json = ""; StringBuilder sbJsonFields = new StringBuilder(); StringBuilder sbDbFields = new StringBuilder(); StringBuilder sb = new StringBuilder(); StringBuilder sbLinkNew = new StringBuilder(); var pageTemplate = SessionService.PageTemplate(pageTemplateId); tableName = pageTemplate.TableName; primaryKey = pageTemplate.PrimaryKey; if (recordId.Length > 0 && recordId != "0") { var dbEntity = SessionService.DbEntity(pageTemplate.DbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; if (pageTemplate.PrimaryKeyType == "TEXT") { recordId = "'" + recordId + "'"; } StringBuilder sbSelect = new StringBuilder(); StringBuilder sbFrom = new StringBuilder(); sbSelect.Append("SELECT " + tableName + "." + primaryKey + " AS " + tableName + "_" + primaryKey); sbFrom.Append(" FROM " + tableName); var columnDefs = SessionService.ColumnDefs(pageTemplateId); foreach (var columnDef in columnDefs.Where(w => !w.IsPrimary)) { if (layoutType == "View" && columnDef.LookupTable.Length > 0 && columnDef.ValueField.Length > 0 && columnDef.TextField.Length > 0) { if (columnDef.IsEncrypted) // decrypt the value { var encryptedValue = targetDb.Database.SqlQuery <string>("SELECT " + columnDef.ColumnName + " FROM " + tableName + " WHERE " + tableName + "." + primaryKey + " = " + recordId).FirstOrDefault(); var decryptedValue = Crypto.Decrypt(encryptedValue); sbSelect.Append(", '" + decryptedValue.Replace("'", "''") + "' AS " + tableName + "_" + columnDef.ColumnName); } else if (columnDef.ElementType == "MultiSelect") { // get items and inject into select statement var lookupValue = targetDb.Database.SqlQuery <string>("SELECT " + columnDef.ColumnName + " FROM " + tableName + " WHERE " + primaryKey + " = " + recordId).FirstOrDefault(); if (lookupValue != null && lookupValue.Length > 0) { var injectSql = "SELECT STUFF((SELECT ', ' + " + columnDef.TextField + " FROM (SELECT " + columnDef.TextField + " FROM " + columnDef.LookupTable + " WHERE " + columnDef.ValueField + " IN (" + lookupValue + ")) AS T FoR XML PATH('')), 1, 1, '') AS InjectValue"; var injectValue = targetDb.Database.SqlQuery <string>(injectSql).FirstOrDefault(); if (injectValue != null) { sbSelect.Append(", '" + injectValue.Replace("'", "''") + "' AS " + tableName + "_" + columnDef.ColumnName); } } } else { sbSelect.Append(", " + columnDef.LookupTable + "." + columnDef.TextField + " AS " + tableName + "_" + columnDef.ColumnName); // append fields from the lookup table var lookupColumnDefs = SessionService.ColumnDefs(pageTemplate.DbEntityId, columnDef.LookupTable); foreach (var lookupColumnDef in lookupColumnDefs) { sbSelect.Append(", " + columnDef.LookupTable + "." + lookupColumnDef.ColumnName + " AS " + columnDef.LookupTable + "_" + lookupColumnDef.ColumnName); } sbFrom.Append(" LEFT JOIN " + columnDef.LookupTable + " ON " + columnDef.LookupTable + "." + columnDef.ValueField + " = " + tableName + "." + columnDef.ColumnName + " "); } } else { if (columnDef.IsEncrypted) // decrypt the value { var encryptedValue = targetDb.Database.SqlQuery <string>("SELECT " + columnDef.ColumnName + " FROM " + tableName + " WHERE " + tableName + "." + primaryKey + " = " + recordId).FirstOrDefault(); var decryptedValue = Crypto.Decrypt(encryptedValue); sbSelect.Append(", '" + decryptedValue.Replace("'", "''") + "' AS " + tableName + "_" + columnDef.ColumnName); } else { sbSelect.Append(", " + tableName + "." + columnDef.ColumnName + " AS " + tableName + "_" + columnDef.ColumnName); } } } var recordSql = sbSelect.ToString() + sbFrom.ToString() + " WHERE " + tableName + "." + primaryKey + " = " + recordId + " FOR JSON PATH"; var recs = targetDb.Database.SqlQuery <string>(recordSql).ToList(); json = ""; foreach (var rec in recs) { json += rec; } if (json.Length > 2) { json = json.Substring(1, json.Length - 2); } return(json); } } else // recordId = 0 { // get defaults for new record sb.Clear(); sb.Append("{\"" + tableName + "_" + primaryKey + "\":0"); List <ColumnDef> columnDefs = new List <ColumnDef>(); columnDefs = SessionService.ColumnDefs(pageTemplateId).Where(w => w.IsRequired == true || (w.DefaultValue != null)).ToList(); foreach (var columnDef in columnDefs) { if (columnDef.ElementType == "Note" || columnDef.ElementType == "FileAttachment") { sbLinkNew.Append("DELETE FROM " + columnDef.ElementType + " WHERE RecordId = -" + (columnDef.ColumnDefId + SessionService.UserId) + ";"); } var defaultValue = columnDef.DefaultValue; if (columnDef.OverideValue != null && columnDef.OverideValue.Length > 0) { defaultValue = columnDef.OverideValue; } if (columnDef.IsRequired || (defaultValue != null && defaultValue.Length > 0)) { sb.Append(",\"" + tableName + "_" + columnDef.ColumnName + "\":"); if (defaultValue != null && defaultValue.Length > 0) { if (columnDef.DataType == "NUMBER") { sb.Append(Helper.ToInt32(defaultValue).ToString()); // add lookup display value if (columnDef.LookupTable != null && columnDef.LookupTable.Length > 0 && Helper.ToInt32(defaultValue) > 0) { string lookUpFields = ""; if (columnDef.TextField.Contains(",")) { lookUpFields = "("; string[] fields = columnDef.TextField.Split(new char[] { ',' }); foreach (var field in fields) { if (lookUpFields.Length < 3) { lookUpFields += columnDef.LookupTable + "." + field; } else { lookUpFields += " + ' - ' + " + columnDef.LookupTable + "." + field; } } lookUpFields += ")"; } else { lookUpFields = "ISNULL(" + columnDef.LookupTable + "." + columnDef.TextField + ",'') "; } var displayValue = DataService.GetStringValue("SELECT " + lookUpFields + " FROM " + columnDef.LookupTable + " WHERE " + columnDef.ValueField + " = " + defaultValue); sb.Append(",\"" + tableName + "_" + columnDef.ColumnName + "_\":\"" + displayValue.Replace("\"", "\"\"") + "\""); } } else if (columnDef.DataType == "DECIMAL" || columnDef.DataType == "CURRENCY") { sb.Append(Helper.ToDecimal(defaultValue).ToString()); } else if (columnDef.DataType == "TEXT" || columnDef.DataType == "MAXTEXT") { sb.Append("\"" + defaultValue.ToString() + "\""); } else if (columnDef.DataType == "BOOLEAN") { if (defaultValue.ToLower() == "true" || defaultValue == "1") { sb.Append("true"); } else { sb.Append("false"); } } else if (columnDef.DataType == "DATE") { if (defaultValue == "getdate()") { sb.Append("\"" + DateTime.Now.ToLongDateString() + "\""); } else { sb.Append("\"" + Helper.ToDateTime(defaultValue).ToLongDateString() + "\""); } } else { sb.Append("\"" + defaultValue.ToString() + "\""); } } else if (columnDef.IsRequired == true) { if (columnDef.DataType == "NUMBER") { sb.Append("0"); } else if (columnDef.DataType == "DECIMAL" || columnDef.DataType == "CURRENCY") { sb.Append("0"); } else if (columnDef.DataType == "TEXT" || columnDef.DataType == "MAXTEXT") { sb.Append("\"\""); } else if (columnDef.DataType == "BOOLEAN") { sb.Append("false"); } else if (columnDef.DataType == "DATE") { sb.Append("\"" + DateTime.Now.ToLongDateString() + "\""); } else { sb.Append("\"\""); } } else { if (columnDef.DataType == "NUMBER") { sb.Append("0"); } else if (columnDef.DataType == "DECIMAL" || columnDef.DataType == "CURRENCY") { sb.Append("0"); } else if (columnDef.DataType == "TEXT" || columnDef.DataType == "MAXTEXT") { sb.Append("\"\""); } else if (columnDef.DataType == "BOOLEAN") { sb.Append("false"); } else if (columnDef.DataType == "DATE") { sb.Append("\"" + DateTime.Now.ToLongDateString() + "\""); } else { sb.Append("\"\""); } } } } sb.Append("}"); json = sb.ToString(); //if (sbLinkNew.Length > 0) //{ // DataService.Execute(sbLinkNew.ToString()); //} } return(json); } catch (Exception ex) { Helper.LogError(ex); } return(""); }
public string GetImportData(int dbEntityId, string tableName) { var sql = ""; var insertStatement = ""; var insertSqlStatement = ""; var buildSql = ""; StringBuilder sb = new StringBuilder(); sb.AppendLine("TRUNCATE TABLE " + tableName); sb.AppendLine("GO"); sb.AppendLine("SET IDENTITY_INSERT " + tableName + " ON;"); sb.AppendLine("GO"); var dbEntity = SessionService.DbEntity(dbEntityId); using (TargetEntities targetDb = new TargetEntities()) { targetDb.Database.Connection.ConnectionString = dbEntity.ConnectionString; // get insert statement sql = "SELECT (SELECT 'INSERT INTO " + tableName + "(' + STUFF((SELECT ', ' + ColumnName FROM (SELECT c.Name AS ColumnName FROM sys.columns c JOIN sys.objects o ON o.object_id = c.object_id WHERE o.name = '" + tableName + "' AND c.is_computed = 0 ) AS T FoR XML PATH('')), 1, 1, '') + ') VALUES' AS ColumnNames)"; insertStatement = targetDb.Database.SqlQuery <string>(sql).FirstOrDefault(); sb.AppendLine(insertStatement); // get insert SQL statement sql = @" SELECT (SELECT 'SELECT ''(''' + STUFF((SELECT ', ' + ValuesString FROM ( SELECT CASE WHEN (is_identity = 1) THEN ' + CAST(' + c.Name + ' AS varchar) + ''' WHEN (system_type_id = 35) THEN ''''''' + REPLACE(CAST(ISNULL(' + c.Name + ','''') AS varchar(max)), '''''''','''''''''''') + ''''''' WHEN (system_type_id = 36) THEN ''''''' + CAST(ISNULL(' + c.Name + ','''') AS nvarchar(36)) + ''''''' WHEN (system_type_id = 40) THEN ''''''' + CAST(ISNULL(' + c.Name + ','''') AS varchar) + ''''''' WHEN (system_type_id = 42) THEN 'datetime2' WHEN (system_type_id = 48) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 52) THEN ''' + CAST(ISNULL(' + c.Name + ', 0) AS varchar) + ''' WHEN (system_type_id = 56) THEN ''' + CAST(ISNULL(' + c.Name + ', 0) AS varchar) + ''' WHEN (system_type_id = 58) THEN 'smalldatetime' WHEN (system_type_id = 59) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 60) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 61) THEN ''''''' + CAST(ISNULL(' + c.Name + ','''') AS varchar) + ''''''' WHEN (system_type_id = 62) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 98) THEN 'sql_variant' WHEN (system_type_id = 99) THEN ''''''' + ISNULL(' + c.Name + ','''') + ''''''' WHEN (system_type_id = 104) THEN ''' + CAST(ISNULL(' + c.Name + ',0) AS varchar) + ''' WHEN (system_type_id = 106) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 108) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 122) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 127) THEN ''' + ISNULL(' + c.Name + ',0) + ''' WHEN (system_type_id = 165) THEN 'varbinary' WHEN (system_type_id = 167) THEN ''''''' + REPLACE(ISNULL(' + c.Name + ',''''), '''''''','''''''''''') + ''''''' WHEN (system_type_id = 173) THEN 'binary' WHEN (system_type_id = 175) THEN ''''''' + ISNULL(' + c.Name + ','''') + ''''''' WHEN (system_type_id = 189) THEN 'timestamp' WHEN (system_type_id = 231) THEN ''''''' + REPLACE(CAST(ISNULL(' + c.Name + ','''') AS varchar(max)), '''''''','''''''''''') + ''''''' WHEN (system_type_id = 239) THEN ''''''' + ISNULL(' + c.Name + ','''') + ''''''' WHEN (system_type_id = 241) THEN ''''''' + ISNULL(' + c.Name + ','''') + ''''''' END AS ValuesString FROM sys.columns c JOIN sys.objects o ON o.object_id = c.object_id WHERE o.name = 'PageTemplate' AND c.is_computed = 0 ) AS T FoR XML PATH('')), 1, 1, '') + '), '' FROM PageTemplate' AS SqlString) "; sql = sql.Replace("PageTemplate", tableName); insertSqlStatement = targetDb.Database.SqlQuery <string>(sql).FirstOrDefault(); insertSqlStatement = insertSqlStatement.Replace("SELECT '(' ''' + CAST(ISNULL(guid", "SELECT '(''' + CAST(ISNULL(guid"); var recs = targetDb.Database.SqlQuery <string>(insertSqlStatement); foreach (var rec in recs) { sb.AppendLine(rec); } buildSql = sb.ToString(); buildSql = buildSql.Substring(0, buildSql.Length - 4); } sb.Clear(); sb.AppendLine(buildSql); sb.AppendLine("GO"); sb.AppendLine("SET IDENTITY_INSERT " + tableName + " OFF;"); sb.AppendLine("GO"); return(sb.ToString()); }