PageRenderTime 249ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/AlterGear.SQLLibrary/SQLList.cs

http://altergearsqlindexer.codeplex.com
C# | 258 lines | 249 code | 9 blank | 0 comment | 0 complexity | 3c80e70fe97823b7e2bcec7fb050175f MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.Sql;
  7. using System.Xml.Serialization;
  8. namespace AlterGear.SQLLibrary
  9. {
  10. [Serializable]
  11. public class SQLList : IComparable<SQLList>
  12. {
  13. [XmlIgnore]
  14. const string SERVER_NAME = "ServerName";
  15. [XmlIgnore]
  16. const string INSTANCE_NAME = "InstanceName";
  17. [XmlIgnore]
  18. const string SCHEMA_DATABASES = "Databases";
  19. [XmlIgnore]
  20. const string DATABASE_NAME = "Database_name";
  21. [XmlIgnore]
  22. const string INDEX_ID = "index_id";
  23. [XmlIgnore]
  24. const string OBJECT_ID = "object_id";
  25. [XmlIgnore]
  26. const string NAME = "name";
  27. [XmlIgnore]
  28. const string AVG_FRAG = "Avg_Fragmentation_In_Percent";
  29. [XmlIgnore]
  30. const string TABLE_NAME = "tableName";
  31. [XmlIgnore]
  32. const string SQL_INDICES = @"SELECT A.object_id, A.index_id, B.[name], Avg_Fragmentation_In_Percent, C.[name] as tableName
  33. FROM sys.dm_db_index_physical_stats (db_id(), NULL,NULL, NULL, NULL) AS A
  34. JOIN sys.indexes B WITH(NOLOCK) ON A.Object_id = B.Object_id
  35. AND A.Index_id = B.Index_id
  36. JOIN sys.Tables C WITH(NOLOCK) ON B.Object_id = C.Object_id
  37. WHERE Avg_Fragmentation_In_Percent >= {0}
  38. AND B.[name] IS NOT NULL
  39. order by Avg_Fragmentation_In_Percent desc";
  40. [XmlIgnore]
  41. const string SQL_REORGANISE = @"ALTER INDEX [{0}] ON [{1}] REORGANIZE";
  42. [XmlIgnore]
  43. const string SQL_REBUILD = @"ALTER INDEX [{0}] ON [{1}] REBUILD";
  44. [XmlIgnore]
  45. private System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder;
  46. [XmlAttribute(DataType="string")]
  47. public string ConnectionString { get; set; }
  48. public static List<string> GetSqlInstances()
  49. {
  50. List<string> lstInstances = new List<string>();
  51. SqlDataSourceEnumerator sqlEnumerator = SqlDataSourceEnumerator.Instance;
  52. DataTable dataTable = sqlEnumerator.GetDataSources();
  53. foreach (DataRow row in dataTable.Rows)
  54. {
  55. lstInstances.Add(row[SERVER_NAME] + "\\" + row[INSTANCE_NAME]);
  56. }
  57. return lstInstances;
  58. }
  59. public static List<string> GetSQLDatabase(string sqlInstance, bool integratedSecurity, string userName, string password)
  60. {
  61. List<string> lstDataBases = new List<string>();
  62. System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
  63. sqlConnBuilder.DataSource = sqlInstance;
  64. sqlConnBuilder.IntegratedSecurity = integratedSecurity;
  65. sqlConnBuilder.UserID = userName;
  66. sqlConnBuilder.Password = password;
  67. string connectionString = sqlConnBuilder.ConnectionString;
  68. using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(connectionString))
  69. {
  70. sqlConn.Open();
  71. DataTable dt = sqlConn.GetSchema(SCHEMA_DATABASES);
  72. foreach (DataRow row in dt.Rows)
  73. {
  74. lstDataBases.Add(row[DATABASE_NAME].ToString());
  75. }
  76. }
  77. return lstDataBases;
  78. }
  79. public bool CanConnect(string sqlInstance, bool integratedSecurity, string userName, string password, string databaseName, int timeout, bool throwException, out Exception exception, out string connectionString)
  80. {
  81. bool canConnect = false;
  82. connectionString = "";
  83. exception = null;
  84. System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
  85. sqlConnBuilder.DataSource = sqlInstance;
  86. sqlConnBuilder.IntegratedSecurity = integratedSecurity;
  87. sqlConnBuilder.UserID = userName;
  88. sqlConnBuilder.Password = password;
  89. sqlConnBuilder.InitialCatalog = databaseName;
  90. sqlConnBuilder.ConnectTimeout = timeout;
  91. try
  92. {
  93. using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(sqlConnBuilder.ConnectionString))
  94. {
  95. sqlConn.Open();
  96. connectionString = sqlConnBuilder.ConnectionString;
  97. this.ConnectionString = connectionString;
  98. canConnect = true;
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. exception = ex;
  104. if(throwException == true)
  105. throw ex;
  106. }
  107. return canConnect;
  108. }
  109. /*
  110. public string SQLServerName()
  111. {
  112. sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  113. return sqlConnBuilder.DataSource;
  114. }
  115. */
  116. [XmlIgnore]
  117. public string DisplaySQLConn
  118. {
  119. get
  120. {
  121. if (!string.IsNullOrEmpty(this.SQLDatabaseName) && !string.IsNullOrEmpty(this.SQLServerName))
  122. return this.SQLServerName + " - " + this.SQLDatabaseName;
  123. else
  124. return "";
  125. }
  126. private set { }
  127. }
  128. [XmlAttribute]
  129. public string SQLServerName
  130. {
  131. get
  132. {
  133. sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  134. return sqlConnBuilder.DataSource;
  135. }
  136. set
  137. { }
  138. }
  139. [XmlAttribute]
  140. public string SQLDatabaseName
  141. {
  142. get
  143. {
  144. sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  145. return sqlConnBuilder.InitialCatalog;
  146. }
  147. set { }
  148. }
  149. /*
  150. public string SQLDatabaseName()
  151. {
  152. sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  153. return sqlConnBuilder.InitialCatalog;
  154. }
  155. */
  156. public System.Data.SqlClient.SqlConnectionStringBuilder GetSqlConnectionStringBuilder()
  157. {
  158. return new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  159. }
  160. public List<IndexInfo> GetIndices()
  161. {
  162. return this.GetIndices(0);
  163. }
  164. public List<IndexInfo> GetIndices(int fragPct)
  165. {
  166. List<IndexInfo> lstIndexInfo = new List<IndexInfo>();
  167. using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(this.ConnectionString))
  168. {
  169. sqlConn.Open();
  170. System.Data.SqlClient.SqlCommand sqlCommand = sqlConn.CreateCommand();
  171. sqlCommand.CommandText = string.Format(SQL_INDICES, fragPct);
  172. System.Data.SqlClient.SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
  173. while (sqlDataReader.Read())
  174. {
  175. string name = sqlDataReader.GetString(sqlDataReader.GetOrdinal(NAME));
  176. string tableName = sqlDataReader.GetString(sqlDataReader.GetOrdinal(TABLE_NAME));
  177. int indexId = sqlDataReader.GetInt32(sqlDataReader.GetOrdinal(INDEX_ID));
  178. int objectId = sqlDataReader.GetInt32(sqlDataReader.GetOrdinal(OBJECT_ID));
  179. double avgFrag = sqlDataReader.GetDouble(sqlDataReader.GetOrdinal(AVG_FRAG));
  180. lstIndexInfo.Add(new IndexInfo(objectId, tableName, indexId, name, (decimal)avgFrag));
  181. }
  182. }
  183. return lstIndexInfo;
  184. }
  185. public void ReorganiseIndex(IndexInfo indexInfo)
  186. {
  187. System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  188. string reorganise = string.Format(SQL_REORGANISE, indexInfo.Name, indexInfo.TableName);
  189. using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(this.ConnectionString))
  190. {
  191. sqlConn.Open();
  192. System.Data.SqlClient.SqlCommand sqlCommand = sqlConn.CreateCommand();
  193. sqlCommand.CommandText = reorganise;
  194. sqlCommand.ExecuteNonQuery();
  195. }
  196. }
  197. public void ReBuildIndex(IndexInfo indexInfo)
  198. {
  199. System.Data.SqlClient.SqlConnectionStringBuilder sqlConnBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);
  200. string reorganise = string.Format(SQL_REBUILD, indexInfo.Name, indexInfo.TableName);
  201. using (System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(this.ConnectionString))
  202. {
  203. sqlConn.Open();
  204. System.Data.SqlClient.SqlCommand sqlCommand = sqlConn.CreateCommand();
  205. sqlCommand.CommandText = reorganise;
  206. sqlCommand.ExecuteNonQuery();
  207. }
  208. }
  209. #region IComparable<SQLList> Members
  210. public int CompareTo(SQLList other)
  211. {
  212. if (this.SQLServerName == other.SQLServerName
  213. && this.SQLDatabaseName == other.SQLDatabaseName)
  214. {
  215. return 0;
  216. }
  217. else
  218. return -1;
  219. }
  220. #endregion
  221. }
  222. }