Пример #1
0
        static void Main(string[] args)
        {
            try
            {
                string Profile = (args.Length > 0) ? args[0] : "default";
                Console.WriteLine(Profile);
                Logfile = Profile + "_mysql.log";
                Inifile = Profile + ".ini";

                if (File.Exists(Inifile) == false)
                {
                    throw new ArgumentException("Profile is not found - " + Inifile);
                }

                Props prop = new Props();
                prop.Load(Inifile);

                URL = prop.Get("url");
                string CSV = prop.Get("csv");
                string SQL = prop.Get("sql");

                //test settings
                if (URL == string.Empty)
                {
                    throw new ArgumentException(@"Setting 'url' is not found in profile");
                }
                if (CSV == string.Empty)
                {
                    throw new ArgumentException(@"Setting 'csv' is not found in profile");
                }
                if (SQL == string.Empty)
                {
                    throw new ArgumentException(@"Setting 'sql' is not found in profile");
                }
                if (File.Exists(SQL) == false)
                {
                    throw new ArgumentException("SQL is not found - " + SQL);
                }

                //load sql
                string[] sql = File.ReadAllLines(SQL, Encoding.UTF8);
                Log("SQL is used - " + SQL);

                // connect to db
                Connect(true);
                Log("Connected - " + conn.State.ToString());

                //get data
                List <String> output = new List <String>();
                StringBuilder sb     = new StringBuilder();

                string sql_string = String.Join(" ", sql);
                sql_string = SqlSpecial(sql_string);


                Connect(true);
                MySqlCommand    cmd    = new MySqlCommand(sql_string, conn);
                MySqlDataReader reader = cmd.ExecuteReader();
                Log("Records - " + reader.HasRows);
                int FieldCount  = reader.FieldCount;
                int OutputCount = 0;
                while (reader.Read())
                {
                    sb.Length = 0;
                    for (int i = 0; i < FieldCount; i++)
                    {
                        sb.Append(reader.GetString(i).Trim());
                        if (i < FieldCount - 1)
                        {
                            sb.Append(";");
                        }
                    }                    //for
                    output.Add(sb.ToString());
                    OutputCount++;
                }                //while

                Log("Records output - " + OutputCount);
                reader.Close();

                File.WriteAllLines(CSV, output.ToArray());
            }            //try
            catch (Exception e)
            {
                Log(e.Message);
            }            //catch
            finally
            {
                Connect(false);
                Log("=========", true);
            }    //finally
        }        //function