Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. //SchemaCreation View
  2. <script type="text/javascript">
  3. var TableWidth = null;
  4. var TableHeight = null;
  5. $(document).ready(function ()
  6. {
  7. //debugger;
  8. TableWidth = $("#list").width();
  9. TableHeight = $("#Div1").height();
  10. });
  11. jQuery(document).ready(function ()
  12. {
  13. //debugger;
  14. jQuery("#list").jqGrid({
  15. url: '/Schema/GetGridData?DataSource=' + '@ViewData["ServerName"]' + '&InitialCatalog=' + '@ViewData["DataBaseName"]' + '&UserName=' + '@ViewData["UserName"]' + '&PassWord=' + '@ViewData["PassWord"]',
  16. datatype: 'json',
  17. mtype: 'GET',
  18. colNames: ['Table Names', 'Column Names'],
  19. colModel: [
  20. { name: 'Table Names', index: 'Table Names', width: 40, align: 'left', Style:'padding-left:15px;'},
  21. { name: 'Column Names', index: 'Column Names', width: 40, align: 'left'},//, width: 80, formatter: 'select', type:'select', edittype: 'select', editoptions: { value: "1:One;2:Two" } }
  22. ],
  23. pager: jQuery('#pager'),
  24. rowNum: 10,
  25. rowList: [5, 10, 20, 50],
  26. sortname: 'Table Names',
  27. sortorder: "desc",
  28. multiselect: true,
  29. viewrecords: true,
  30. width: TableWidth,
  31. Height: 500,
  32. scroll: true,
  33. //caption: 'My first grid'
  34. });
  35. });
  36. </script>
  37.  
  38.  
  39.  
  40. //SchemaController:-
  41.  
  42. public ActionResult GetGridData(string sidx, string sord, int page, int rows, string DataSource, string InitialCatalog, string UserName, string PassWord)
  43. {
  44. DataSource = CrossCutting.EncryptDecrypt.Decrypt(DataSource, ConfigurationManager.AppSettings["EncryptDecryptKey"]);
  45. InitialCatalog = CrossCutting.EncryptDecrypt.Decrypt(InitialCatalog, ConfigurationManager.AppSettings["EncryptDecryptKey"]);
  46. UserName = CrossCutting.EncryptDecrypt.Decrypt(UserName, ConfigurationManager.AppSettings["EncryptDecryptKey"]);
  47. PassWord = CrossCutting.EncryptDecrypt.Decrypt(PassWord, ConfigurationManager.AppSettings["EncryptDecryptKey"]);
  48.  
  49. return Content(JsonHelper.JsonForJqgrid(GetDataTable(sidx, sord, page, rows, DataSource, InitialCatalog, UserName, PassWord), rows, GetTotalCount(DataSource, InitialCatalog, UserName, PassWord), page), "application/json");
  50. }
  51.  
  52. public DataTable GetDataTable(string sidx, string sord, int page, int pageSize, string DataSource, string InitialCatalog, string UserName, string PassWord)
  53. {
  54. int startIndex = (page - 1) * pageSize;
  55. int endIndex = page * pageSize;
  56. string sql = "select name from sys.tables";
  57. DataTable dt = new DataTable();
  58. string connString = "Data Source=" + DataSource + ";Initial Catalog=" + InitialCatalog + ";User ID=" + UserName + ";pwd=" + PassWord;
  59. SqlConnection conn = new SqlConnection(connString);
  60. SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
  61. var rows = adap.Fill(dt);
  62. return dt;
  63. }
  64.  
  65. public int GetTotalCount(string DataSource, string InitialCatalog, string UserName, string PassWord)
  66. {
  67. SqlConnection sqlConnection2 = null;
  68. string connString = "Data Source=" + DataSource + ";Initial Catalog=" + InitialCatalog + ";User ID=" + UserName + ";pwd=" + PassWord;
  69. try
  70. {
  71. string cmdText = "select name from sys.tables";
  72. List<string> tablenames = new List<string>();
  73. using (sqlConnection2 = new SqlConnection(connString))
  74. {
  75. sqlConnection2.Open();
  76. using (SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection2))
  77. {
  78. SqlDataReader reader = sqlCmd.ExecuteReader();
  79. while (reader.Read())
  80. {
  81. tablenames.Add(reader.GetValue(0).ToString());
  82. //Count = (int)sqlCmd.ExecuteScalar();
  83. }
  84. }
  85. }
  86. return tablenames.Count();
  87. }
  88. catch
  89. {
  90. }
  91. finally
  92. {
  93. try
  94. {
  95. if (ConnectionState.Closed != sqlConnection2.State)
  96. {
  97. sqlConnection2.Close();
  98. }
  99. }
  100. catch
  101. {
  102. }
  103. }
  104. return -1;
  105. }
  106.  
  107.  
  108. //JsonHelper.cs:-
  109.  
  110. using System;
  111. using System.Collections.Generic;
  112. using System.Data;
  113. using System.Data.SqlClient;
  114. using System.Linq;
  115. using System.Text;
  116. using System.Web;
  117.  
  118. namespace RuleEngine.Helper
  119. {
  120. public class JsonHelper
  121. {
  122. public static string JsonForJqgrid(DataTable dt, int pageSize, int totalRecords, int page)
  123. {
  124. string connString = "Data Source=" + "po8lt8zdqt.database.windows.net" + ";Initial Catalog=" + "iNubeRSBYClaims2" + ";User ID=" + "inubeadmin" + ";pwd=" + "Ravi*vikram123";
  125. string cmdText = "SELECT table_name,column_name FROM information_schema.columns Group By table_name,column_name ORDER BY table_name,column_name";
  126.  
  127. List<SchemaCreation> AllTableAndColumnsName = new List<SchemaCreation>();
  128. using (SqlConnection SqlConn = new SqlConnection(connString))
  129. {
  130. SqlConn.Open();
  131. using (SqlCommand SqlCmd = new SqlCommand(cmdText, SqlConn))
  132. {
  133. SqlDataReader Reader = SqlCmd.ExecuteReader();
  134. while (Reader.Read())
  135. {
  136. AllTableAndColumnsName.Add(new SchemaCreation { Table_Names = Reader.GetValue(0).ToString(), Column_Names = Reader.GetValue(1).ToString() });
  137. }
  138. }
  139. }
  140.  
  141. //List<string> ColumnNames = new List<string>();
  142. int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
  143. StringBuilder jsonBuilder = new StringBuilder();
  144. jsonBuilder.Append("{");
  145. jsonBuilder.Append(""total":" + totalPages + ","page":" + page + ","records":" + (totalRecords) + ","rows"");
  146. jsonBuilder.Append(":[");
  147. for (int i = 0; i < dt.Rows.Count; i++)
  148. {
  149. jsonBuilder.Append("{"i":" + (i) + ","cell":[");
  150. for (int j = 0; j < dt.Columns.Count; j++)
  151. {
  152. jsonBuilder.Append(""");
  153. jsonBuilder.Append(dt.Rows[i][j].ToString());
  154. jsonBuilder.Append("",");
  155.  
  156. List<string> ColumnNames = new List<string>();
  157. string EachTableName = dt.Rows[i][j].ToString();
  158. ColumnNames = AllTableAndColumnsName.Where(p => p.Table_Names == EachTableName).Select(sw => sw.Column_Names).ToList();
  159.  
  160. jsonBuilder.Append(""");
  161. foreach (var EachColName in ColumnNames)
  162. {
  163. jsonBuilder.Append(EachColName);
  164. jsonBuilder.Append(",");
  165. }
  166. jsonBuilder.Append("",");
  167.  
  168. }
  169. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  170. jsonBuilder.Append("]},");
  171. }
  172. jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
  173. jsonBuilder.Append("]");
  174. jsonBuilder.Append("}");
  175. return jsonBuilder.ToString();
  176. }
  177. }
  178.  
  179. public class SchemaCreation
  180. {
  181. public string Table_Names { get; set; }
  182.  
  183. public string Column_Names { get; set; }
  184.  
  185. public List<string> ColumnNames { get; set; }
  186. }
  187.  
  188. }
  189.  
  190. colModel: [
  191. { name: 'TableName', index: 'TableName', width: 40, align: 'left',
  192. Style:'padding-left:15px;'},
  193. { name: 'ColumnName', index: 'ColumnName', width: 40, align: 'left',
  194. width: 80, formatter: 'select', type:'select', edittype: 'select',
  195. editoptions: { value: "1:One;2:Two" } }
  196. ]
  197.  
  198. colModel: [
  199. { name: 'Table_Names', index: 'Table_Names', width: 40, align: 'left', Style:'padding-left:15px;'},
  200. { name: 'Column_Names', index: 'Column_Names', width: 40, align: 'left', width: 80, formatter: 'select', type:'select', edittype: 'select',
  201. editoptions:{
  202. dataUrl: '/Schema/GetGridData?DataSource=' + '@ViewData["ServerName"]' + '&InitialCatalog=' + '@ViewData["DataBaseName"]' + '&UserName=' + '@ViewData["UserName"]' + '&PassWord=' + '@ViewData["PassWord"]',
  203. buildSelect: function (data) {
  204. var response, s = '<select>', i;
  205. response = jQuery.parseJSON(data);
  206. if (response && response.length) {
  207. $.each(response, function (i) {
  208. s += '<option value="' + this.Column_Names + '">' + this.Column_Names+ '</option>';
  209. });
  210. }
  211. return s + '</select>';
  212. }
  213. }}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement