コード例 #1
0
        public IDbConnection CloneCurrentConnection(string database)
        {
            SqlConnectionInfo connectionInfo = GetCurrentSqlConnectionInfo(database);
            IDbConnection     connection     = connectionInfo.CreateConnectionObject();

            return(connection);
        }
コード例 #2
0
 public SqlConnInfo(SqlConnectionInfo info)
 {
     info.ApplicationName = ApplicationName;
     _handler             = delegate
     {
         return(info.CreateConnectionObject());
     };
 }
コード例 #3
0
        void PreserveSQLHistory(ref string _SQLTable)
        {
            string sHistorySuffix = "History";

            if (_NumInstance > 1)
            {
                sHistorySuffix = "_History";
            }
            StringBuilder messageText = new StringBuilder();

            try
            {
                SqlConnectionInfo connInfo = new SqlConnectionInfo(Properties.Settings.Default.SQLServer);
                IDbConnection     conn     = connInfo.CreateConnectionObject();
                conn.Open();
                conn.ChangeDatabase(Properties.Settings.Default.SQLServerDatabase);

                string sSQL = @"if object_id('[" + _SQLTable.Replace("'", "''") + @"]') is not null 
                                 select top 1 * from [" + _SQLTable.Replace("]", "]]") + "]";
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sSQL, (System.Data.SqlClient.SqlConnection)conn);
                cmd.CommandTimeout = 0;
                System.Data.SqlClient.SqlDataReader datareader = cmd.ExecuteReader();
                string sColumns = "";
                for (int i = 0; i < datareader.FieldCount; i++)
                {
                    if (!string.IsNullOrEmpty(sColumns))
                    {
                        sColumns += ", ";
                    }
                    sColumns += "[" + datareader.GetName(i) + "]";
                }
                datareader.Close();

                if (sColumns != "")
                {
                    sSQL = @"
                                if object_id('[" + _SQLTable.Replace("'", "''") + @"]') is not null
                                begin
                                    if object_id('[" + _SQLTable.Replace("'", "''") + sHistorySuffix + @"]') is null
                                    begin
                                        select * into [" + _SQLTable.Replace("]", "]]") + sHistorySuffix + "] from [" + _SQLTable.Replace("]", "]]") + @"]
                                    end
                                    else
                                    begin
                                        SET IDENTITY_INSERT [" + _SQLTable.Replace("]", "]]") + sHistorySuffix + @"] ON
                                        insert into [" + _SQLTable.Replace("]", "]]") + sHistorySuffix + "] (" + sColumns + @")
                                        select * from [" + _SQLTable.Replace("]", "]]") + @"]
                                        SET IDENTITY_INSERT [" + _SQLTable.Replace("]", "]]") + sHistorySuffix + @"] OFF
                                    end
                                end
                                ";

                    cmd.CommandText = sSQL;
                    int iRowsPreserved = cmd.ExecuteNonQuery();

                    messageText.Append(DateTime.Now.ToString() + ":  Successfully preserved " + iRowsPreserved + " rows of history to table: " + _SQLTable + sHistorySuffix);
                    WriteLog(messageText.ToString());
                    EventLog.WriteEntry(this.ServiceName, messageText.ToString(), EventLogEntryType.Information);
                }

                conn.Close();
                conn.Dispose();
            }
            catch (Exception ex)
            {
                messageText.Append(DateTime.Now.ToString() + ":  Cannot preserve history of trace table. ").AppendLine();
                messageText.Append("Error: " + ex.Message).AppendLine();
                messageText.Append(ex.StackTrace).AppendLine();

                while (ex.InnerException != null)
                {
                    messageText.Append("INNER EXCEPTION: ");
                    messageText.Append(ex.InnerException.Message).AppendLine();
                    messageText.Append(ex.InnerException.StackTrace).AppendLine();

                    ex = ex.InnerException;
                }

                WriteLog(messageText.ToString());
                EventLog.WriteEntry(this.ServiceName, messageText.ToString(), EventLogEntryType.Warning);
            }
        }