пʼятниця, 19 серпня 2016 р.

How to create database in Microsoft SQL Server and view via Visual Studio

1. Download SQL Server Express
2. Download Microsoft SQL Server Management Studio
3. On SQL Express window click on Connect now button.
or
in cmd type sqlcmd -S <ComputerName>  
As result sqlcmd window will be opend
type create database NewDB
4. type go
5. type  use NewDB
6. type go
7. type create table NewTable ( columnname1 columntype1, columnname2 columntype2)
8. type go
9. In Management Studio click on File/Connect Object Explorer
    type server name and authentication
If DB created locally you can find server name on the tpo of sqlcmd
If DB created locally authentification will be Windows Authentification
10. in Visual Studio Tools/Connect to Database type server name and authentication

четвер, 18 серпня 2016 р.

How set/get data to cache c#

1. create helper class
 public static class CacheHelper
    {
        static readonly ObjectCache Cache = MemoryCache.Default;

        public static void SaveTocache(string cacheKey, object savedItem, DateTime absoluteExpiration)
        {
            MemoryCache.Default.Set(cacheKey, savedItem, absoluteExpiration);
        }

        public static T GetFromCache<T>(string cacheKey) where T : class
        {
            return MemoryCache.Default[cacheKey] as T;
        }

        public static void RemoveFromCache(string cacheKey)
        {
            MemoryCache.Default.Remove(cacheKey);
        }

        public static bool IsIncache(string cacheKey)
        {
            return MemoryCache.Default[cacheKey] != null;
        }

        public static T Get<T>(string cacheKey, string regionName = null) where T : class
        {
           
            return MemoryCache.Default.Get(cacheKey) as T;
        }

        /// <summary>
        /// Gets all cached items as a list by their key.
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAll()
        {
            return Cache.Select(keyValuePair => keyValuePair.Key).ToList();
        }
    }

2. in base class add

        /// <summary>
        ///     The general test clean.
        /// </summary>
        [TestCleanup]
        public void GeneralTestClean()
        {
            this.TestContextWithTimer.EndTimer(TestName);
           
            //Create a custom Timeout of 10 seconds
            DateTime customTimeout = DateTime.Now.AddSeconds(60.0);
            testDetailed.TestName = TestName;
            testDetailed.Date = DateTime.Now;
           
            testDetailed.Duration = this.TestContextWithTimer.TestDuration(testName).ToString();
            testDetailed.PassFail = (Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome.Passed == this.TestContext.CurrentTestOutcome ? "Pass" : "Fail");
            result.Add(testDetailed);

            CacheHelper.SaveTocache(CacheKeyValue, result, customTimeout);

            //get list of keys
            List<string> getCacheKeyList = CacheHelper.GetAll();
            getCacheValueList = new Dictionary<string, List<string>>();
            foreach (var item in getCacheKeyList)
            {
                var getCacheValueFromCache = CacheHelper.GetFromCache<List<string>>(item);
                getCacheValueList.Add(item, getCacheValueFromCache);
            }
         
           this.client.Dispose();
        }

        private readonly string CacheKeyValue = "TestResultDetails " + DateTime.Now;

        private static Dictionary<string, List<string>> getCacheValueList;

        private static List<TestDetailed> endResult = new List<TestDetailed>();

        [AssemblyCleanup]
        public static void ClassCleanup()
        {                            
            foreach (string key in getCacheValueList.Keys)
            {
                foreach (string val in getCacheValueList[key])
                {
                    Console.WriteLine(val);
                }
            }        
        }