Пример #1
0
        public override void Execute(Dispatcher service)
        {
            var sql = new SqlService();

            sql.ConnectionParams = new DbConnectionParams
            {
                ConnectionString = ConnectionString
            };
            Console.WriteLine("Executing '" + SqlQuery + "'...");
            var res = sql.RunSql(SqlQuery, null, true);

            if (res.IsSuccess)
            {
                Console.WriteLine("Success : " + res.Message);
            }
            else
            {
                using (var set = ColorSetter.Set(ConsoleColor.Red))
                {
                    Console.Write("Failed");
                    Console.WriteLine(res.ExceptionMessage);
                }
            }
            Console.WriteLine();
        }
Пример #2
0
 protected IEnumerable <T> GetDataAs <T>(string sql, string database = null)
 {
     ConnectionParams.Database = database;
     using (SqlConnection conn = new SqlConnection(ConnectionParams.ConnectionString))
     {
         try
         {
             List <T> lst = new List <T>();
             conn.Open();
             SqlCommand cmd = new SqlCommand(sql, conn);
             if (CommandTimeout != 0)
             {
                 cmd.CommandTimeout = CommandTimeout;
             }
             var red   = cmd.ExecuteReader();
             var props = typeof(T).GetProperties().ToDictionary(d => d.Name);
             while (red.Read())
             {
                 var inst = Activator.CreateInstance <T>();
                 for (var i = 0; i < red.FieldCount; i++)
                 {
                     var name = red.GetName(i);
                     if (props.TryGetValue(name, out PropertyInfo inf))
                     {
                         var s = red.GetValue(i);
                         if (s.GetType() != typeof(DBNull))
                         {
                             inf.SetValue(inst, Convert.ChangeType(s, inf.PropertyType));
                         }
                     }
                 }
                 lst.Add(inst);
             }
             conn.Close();
             conn.Dispose();
             return(lst);
         }
         catch (Exception ex)
         {
             Console.WriteLine("Batch failed");
             Console.WriteLine(sql);
             using (ColorSetter.Set(ConsoleColor.Red))
                 Console.WriteLine(ex.GetMessageRecursive());
             conn.Close();
             throw;
         }
     }
 }