CHAPTER 8 ■ SQL AZURE 273 public class SQLDataAccessHelper { protected SQLDatabaseAccessRoot sqlDatabaseAccessRoot = null; #region Constructors public SQLDataAccessHelper(SQLDatabaseAccessRoot sqlDatabaseAccessRoot) { sqlDatabaseAccessRoot = sqlDatabaseAccessRoot; } public SQLDataAccessHelper(SQLDatabaseAccessRoot sqlDatabaseAccessRoot, string connectionString) : this(sqlDatabaseAccessRoot) { ConnectionString = connectionString; } #endregion #region Properties public string ConnectionString { get; set; } public string ConnectedDatabase { get { string database = string.Empty; string[] split = ConnectionString.StartsWith("Server")? Regex.Split(ConnectionString, "Server=") :Regex.Split(ConnectionString, "Data Source="); if (null != split && split.Length > 1) { string strValue = split[split.Length - 1]; int indexEnd = strValue.IndexOf(';'); try { database = strValue.Substring(0, indexEnd); } catch { } } return database; } } #endregion #region Public Methods public StringBuilder Execute(string subject, ref Object results) CHAPTER 8 ■ SQL AZURE 274 { StringBuilder sb = null; if (null != sqlDatabaseAccessRoot) { SQLDatabaseAccessRootSqlDataService dataService = this. sqlDatabaseAccessRoot.SqlDataService .FirstOrDefault<SQLDatabaseAccessRootSqlDataService>( x => x.Subject == subject); if (null != dataService) { if (dataService.Command.Type == SQLDatabaseAccessRootSqlDataServiceCommandType.Query) { sb = ExecuteQuery(dataService.Command.Text); } else if (dataService.Command.Type == SQLDatabaseAccessRootSqlDataServiceCommandType.Storedprocedure) { sb = ExecuteStoredProcedure(dataService); } } } else { throw new ApplicationException( string.Format(" SqlDatabaseService:Query, Subject = <{0}>, SqlDatabaseService is not initialized correctly.", subject)); } return sb; } public StringBuilder ExecuteSelected(string selectedText) { return ExecuteQuery(selectedText); } public bool CreateStoredProcedure(string storedProcedureName, string storedProcedureBody, SQLDatabaseAccessRootSqlDataServiceCommand serviceCommand) { bool success = false; StoredProcedure stroredProcedure = null; if (null != storedProcedureName && null != storedProcedureBody && null != serviceCommand && string.Empty != storedProcedureName && string.Empty != storedProcedureBody) { try { CHAPTER 8 ■ SQL AZURE 275 SqlConnection connection = new SqlConnection(ConnectionString); if (null != connection) { connection.Open(); Server server = new Server(new ServerConnection( connection)); Database db = server.Databases[ConnectedDatabase]; stroredProcedure = new StoredProcedure(db, storedProcedureName); stroredProcedure.TextMode = false; stroredProcedure.AnsiNullsStatus = false; stroredProcedure.QuotedIdentifierStatus = false; stroredProcedure.TextBody = storedProcedureBody; if (null != serviceCommand) { this. PopulateStoredProcedureParameters(ref stroredProcedure, serviceCommand); } stroredProcedure.Create(); success = stroredProcedure == null ? false : true; } } catch (Exception ex) { string msg = string.Empty; if (null != ex.InnerException) { msg = ex.InnerException.Message; if (ex.InnerException.InnerException != null) { msg = ex.InnerException.InnerException.Message; if ( msg.Equals( string.Format( "There is already an object named '{0}' in the database.", storedProcedureName ) ) ) { success = true; } } } else { msg = ex.Message; } } } CHAPTER 8 ■ SQL AZURE 276 return success; } static public object XmlRetrive(Type type, XmlDocument xmlDoc) { object o = null; if (null != xmlDoc && null != xmlDoc.DocumentElement) { XmlSerializer serializer = new XmlSerializer(type); StringReader reader = new StringReader(xmlDoc.OuterXml); try { o = serializer.Deserialize(reader); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.WriteLine(e.StackTrace); throw e; } } return o; } static public StringBuilder XmlPersist(object o, Type type) { XmlSerializer serializer = new XmlSerializer(type); StringBuilder sb = new StringBuilder(); StringWriter writer = new StringWriter(sb); try { serializer.Serialize(writer, o); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.WriteLine(e.StackTrace); throw e; } return writer.GetStringBuilder(); } #endregion #region Private Methods CHAPTER 8 ■ SQL AZURE 277 protected StringBuilder ExecuteQuery(string commandText) { int rowAffected = 0; DataTable datatable = new DataTable(); StringBuilder sb = new StringBuilder(); SQLDataAccessComponent dac = null; SqlCommand Command = null; try { dac = new SQLDataAccessComponent(this.ConnectionString); Command = new SqlCommand(commandText, new SqlConnection()); dac.BeginTrans(); rowAffected = dac.ExecuteDataTable(Command, ref datatable); dac.CommitTrans(); if (datatable.Rows.Count > 0) { foreach (DataRow dataRow in datatable.Rows) { StringBuilder rowBuilder = new StringBuilder(); foreach (object obj in dataRow.ItemArray) { if (!(obj is DBNull || (obj is System.Byte[]))) { rowBuilder.Append(string.Format("{0} ", obj.ToString())); } } sb.Append(string.Format("{0}{1}", rowBuilder.ToString(), Environment.NewLine)); } } sb.Append( string.Format(" SQL execute success, row affected : {0} {1}", rowAffected, Environment.NewLine)); sb.Append(Environment.NewLine); } catch (Exception ex) { throw new ApplicationException( string.Format(" SQL execute failed, error message: {0}", ex.Message, Environment.NewLine)); } finally { if (null != Command) { Command.Dispose(); } if (null != dac) {