コード例 #1
0
 /// <summary>
 /// Start a database transaction with isolation level specified in <see cref="MdbContextOptions.TransactionIsolationLevel"/>
 /// </summary>
 public void BeginTransaction()
 {
     if (_transaction == null)
     {
         if (Connection.State != ConnectionState.Open)
         {
             Connection.Open();
         }
         _transaction = Connection.BeginTransaction(MdbContextOptions.GetOptions().TransactionIsolationLevel);
         _transactionLevel++;
     }
     else
     {
         _transactionLevel++;
     }
 }
コード例 #2
0
        private DbCommand GetCommandInternal(string sql, params MdbParameter[] parameters)
        {
            if (Connection.State != ConnectionState.Open)
            {
                Connection.Open();
            }
            //!!! not work for [procedure name]
            CommandType ct = !sql.Contains(' ') ? CommandType.StoredProcedure : CommandType.Text;

            List <MdbParameter> pFact = new List <MdbParameter>();

            foreach (var param in parameters)
            {
                bool replace = sql.IndexOf(param.Name) > -1;
                if (replace)
                {
                    object p      = param.Value;
                    string pValue = string.Empty;
                    if (p == null)
                    {
                        pValue = "NULL";
                    }
                    else
                    {
                        Type t = p.GetType();
                        if (t.IsNumeric(NumericTypesScope.FloatingPoint))
                        {
                            pValue = p.ToString().Replace(',', '.');
                        }
                        else if (t.IsNumeric())
                        {
                            pValue = p.ToString();
                        }
                        else if (t == typeof(DateTime))
                        {
                            replace = false;
                        }
                        else if (t == typeof(string))
                        {
                            pValue = "'" + p.ToString().Replace("'", "''") + "'";
                        }
                        else if (t != typeof(byte[]) && t.GetInterfaces().FirstOrDefault(type => type.Name == "IEnumerable`1") != null)
                        {
                            pValue = Array2List(p);
                        }
                        else
                        {
                            replace = false;
                        }
                    }

                    if (replace)
                    {
                        sql = sql.Replace(param.Name, pValue);
                    }
                    else
                    {
                        pFact.Add(param);
                    }
                }
                else
                {
                    pFact.Add(param);
                }
            }

            DbCommand command = Factory.CreateCommand(Connection, sql);

            command.CommandType    = ct;
            command.CommandTimeout = MdbContextOptions.GetOptions().CommandTimeout;
            if (_transaction != null)
            {
                command.Transaction = _transaction;
            }

            foreach (var param in pFact)
            {
                command.AddParameter(param.Name, param.Value);
            }
            return(command);
        }