Exemple #1
0
        public static int exec(string sql, bool getidentity = false, int?timeout = null)
        {
            db_conn cnn = null;

            try { cnn = open_conn(); return(cnn.exec(sql, getidentity, timeout)); } finally { if (cnn != null)
                                                                                              {
                                                                                                  cnn.close_conn();
                                                                                              }
            }
        }
Exemple #2
0
        public static object exec_scalar(string sql)
        {
            db_conn cnn = null;

            try { cnn = open_conn(); return(cnn.exec_scalar(sql)); } finally { if (cnn != null)
                                                                               {
                                                                                   cnn.close_conn();
                                                                               }
            }
        }
Exemple #3
0
        public static long exec_identity(string sql)
        {
            db_conn cnn = null;

            try { cnn = open_conn(); return(cnn.exec(sql, true)); } finally { if (cnn != null)
                                                                              {
                                                                                  cnn.close_conn();
                                                                              }
            }
        }
Exemple #4
0
        public static void exec_multi(string sql, db_conn db = null, int timeout = -1)
        {
            bool close = db == null ? true : false;

            try {
                StringBuilder sb    = new StringBuilder();
                string[]      lines = sql.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string ln in lines)
                {
                    // go
                    if (ln.Trim().ToLower().Replace(" ", "") == "go")
                    {
                        if (db == null)
                        {
                            log.log_debug("open default conn", true); db = dal.open_conn(timeout: timeout); close = true;
                        }
                        log.log_sql(sb.ToString(), true);
                        db.exec(sb.ToString());
                        sb.Clear();
                    } // ##conn.name:
                    else if (ln.Length > 12 && ln.Substring(0, 12) == "##conn.name:")
                    {
                        if (db != null && close)
                        {
                            log.log_debug("close conn", true); db.close_conn();
                        }
                        log.log_txt("open conn: " + ln.Substring(12), true);
                        db = dal.open_conn(conn_name: ln.Substring(12)); close = true;
                    }
                    else
                    {
                        sb.Append(ln + "\r\n");
                    }
                }

                // ultima query
                if (sb.Length > 0)
                {
                    if (db == null)
                    {
                        log.log_debug("open default conn", true); db = dal.open_conn(timeout: timeout); close = true;
                    }
                    log.log_sql(sb.ToString(), true);
                    db.exec(sb.ToString());
                    sb.Clear();
                }
            } catch (Exception ex) { Console.WriteLine(ex.Message); throw ex; } finally { if (db != null && close)
                                                                                          {
                                                                                              Console.WriteLine("close conn"); db.close_conn();
                                                                                          }
            }
        }
Exemple #5
0
        public static DataTable exec_sp(string sp, List <db_par> pars, int timeout = -1, string conn = "")
        {
            db_conn cnn = null;

            try {
                cnn = conn == "" ? open_conn() : open_conn(conn_name: conn);
                return(cnn.dt_sp(sp, pars, timeout));
            } finally { if (cnn != null)
                        {
                            cnn.close_conn();
                        }
            }
        }
Exemple #6
0
        public static db_conn open_conn(bool begin_trans = false, string conn_name = "", int timeout = -1, bool no_log = false)
        {
            string  db_name = conn_name == "" ? cfg.get_key("conn_name") : conn_name;
            db_conn db      = new db_conn(db_name, cfg.get_conn(db_name), cfg.get_conn_provider(db_name), timeout);

            if (!begin_trans)
            {
                db.open_conn(no_log);
            }
            else
            {
                db.open_conn_trans(no_log);
            }
            return(db);
        }
Exemple #7
0
        public static long exec_qrys(string[] sqls, db_conn c = null)
        {
            long    nr  = 0;
            db_conn cnn = null;

            try {
                cnn = c == null?open_conn() : c;

                foreach (string sql in sqls)
                {
                    nr += cnn.exec(sql);
                }
            } finally { if (cnn != null && c == null)
                        {
                            cnn.close_conn();
                        }
            }

            return(nr);
        }
Exemple #8
0
        public static DataTable dt_sql(string sql, bool add_row, out DbDataAdapter ad, string conn = "", int timeout = -1, bool no_log = false)
        {
            db_conn cnn = null;

            try {
                cnn = open_conn(conn_name: conn, timeout: timeout, no_log: no_log);

                DataTable tbl = cnn.dt_table(sql, "TABLE", out ad, no_log: no_log);
                if (add_row)
                {
                    tbl.Rows.InsertAt(tbl.NewRow(), 0);
                    tbl.AcceptChanges();
                }

                return(tbl);
            } finally { if (cnn != null)
                        {
                            cnn.close_conn(no_log);
                        }
            }
        }
Exemple #9
0
        public static db_conn open_conn(bool begin_trans = false, string conn_name = "", int timeout = -1, bool no_log = false)
        {
            if (_conn != null)
            {
                if (!_conn.is_opened())
                {
                    _conn.open_conn();
                }
                return(_conn);
            }

            db_conn db = new db_conn(_conn_name, _conn_string, _provider, timeout);

            if (!begin_trans)
            {
                db.open_conn(no_log);
            }
            else
            {
                db.open_conn_trans(no_log);
            }
            return(db);
        }
Exemple #10
0
 public static void exec_script(string script, db_conn db = null, int timeout = -1)
 {
     exec_multi(System.IO.File.ReadAllText(script), db, timeout);
 }
Exemple #11
0
 public static void init(db_conn c)
 {
     _conn = c;
 }