Exemplo n.º 1
0
        public int ExecuteSqlTran(List <string> SQLStringList)
        {
            MySQLConnection connection = this.DbConn;
            int             result;

            using (MySQLCommand cmd = new MySQLCommand())
            {
                cmd.Connection = connection;
                DbTransaction tx = connection.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    int count = 0;
                    for (int i = 0; i < SQLStringList.Count; i++)
                    {
                        string strsql = SQLStringList[i];
                        if (strsql.Trim().Length > 1)
                        {
                            cmd.CommandText = strsql;
                            count          += cmd.ExecuteNonQuery();
                            this.LogSql(strsql);
                        }
                    }
                    tx.Commit();
                    result = count;
                }
                catch
                {
                    tx.Rollback();
                    result = -1;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Executes a transaction (a sequence of SQL sentences) from
 /// <para/>the "buffer". After completion of the "buffer" excecution,
 /// <para/>the "buffer" is cleared and buffering is disabled.
 /// </summary>
 public void EndBufferMySQL()
 {
     if (buffer)
     {
         buffer = false;
         using (MySqlTransaction trans = MySQLConnection.BeginTransaction())
         {
             using (MySqlCommand command = new MySqlCommand(sql_entry, MySQLConnection))
             {
                 command.CommandText = sql_entry;
                 command.ExecuteNonQuery();
             }
             trans.Commit();
         }
         sql_entry = string.Empty;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// The most important method in LangLib. This creates the database,
        /// <para/>the tables to it (MESSAGES, FORMITEMS, CULTURES).
        /// <para/><para/>Also the FallBackCulture is updated for the underlying form/window.
        /// <para/>Messages from the given <paramref name="messageResource"/> are inserted to
        /// <para/>the database if their don't exists.
        /// <para/><para/>The table fields FORMITEMS.INUSE and MESSAGES.INUSE are updated
        /// <para/>for the FallBackCulture.
        /// </summary>
        /// <param name="messageResource">A resource name that contains the application
        /// <para/>messages in the fall FallBackCulture language.
        /// <para/>For example if I have an application which assembly name is
        /// <para/>LangLibTestWinforms and it has a .resx file called Messages
        /// <para/>I would give this parameter a value of "LangLibTestWinforms.Messages".</param>
        /// <param name="culture">The culture to use for the localization.
        /// <para/>If no culture is given the current system culture is used and
        /// <para/>the FallBackCulture is used as fallback culture.</param>
        /// <param name="loadItems">To load language items or not.</param>
        public void InitalizeLanguageMySQL(string messageResource, CultureInfo culture = null, bool loadItems = true)
        {
            if (culture == null)
            {
                culture = CultureInfo.CurrentCulture;
            }

            if (!Design)
            {
                try
                {
                    DateTime dt = DateTime.Now;
                    if (!Directory.Exists(DataDir))
                    {
                        Directory.CreateDirectory(DataDir);
                    }

                    if (MySQLConnection == null)
                    {
                        string connStr = dbConnectionStr == string.Empty ? string.Format("Server={0};Port={1};Database={2};Uid={3};Pwd={4};CharSet=utf8;", dbHost, dbPort, dbName, dbUser, dbPassword) : dbConnectionStr;
                        MySQLConnection = new MySqlConnection(connStr);
                        MySQLConnection.Open();
                    }

                    if (!tablesCreated && !dbNoTables)
                    {
                        using (MySqlCommand command = new MySqlCommand(TableCreateMessages, MySQLConnection))
                        {
                            command.ExecuteNonQuery();
                        }

                        using (MySqlCommand command = new MySqlCommand(TableCreateFormItems, MySQLConnection))
                        {
                            command.ExecuteNonQuery();
                        }

                        using (MySqlCommand command = new MySqlCommand(TableCreateCultures, MySQLConnection))
                        {
                            command.ExecuteNonQuery();
                        }
                        tablesCreated = true;
                    }

                    if (!culturesInserted && !loadItems)
                    {
                        string        sql = string.Empty;
                        CultureInfo[] allCultures;
                        allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

                        foreach (CultureInfo ci in allCultures)
                        {
                            sql += InsertCulture(ci);
                        }


                        using (MySqlTransaction trans = MySQLConnection.BeginTransaction())
                        {
                            using (MySqlCommand command = new MySqlCommand(sql, MySQLConnection))
                            {
                                command.CommandText = sql;
                                command.ExecuteNonQuery();
                            }
                            trans.Commit();
                        }
                        culturesInserted = true;
                    }

                    if (!loadItems)
                    {
                        GetGuiObjets(fallBackCulture);
                        if (!langUseUpdated)
                        {
                            using (MySqlTransaction trans = MySQLConnection.BeginTransaction())
                            {
                                using (MySqlCommand command = new MySqlCommand("UPDATE " + Schema + "FORMITEMS SET INUSE = 0 WHERE CULTURE = '" + fallBackCulture + "' ", MySQLConnection))
                                {
                                    command.ExecuteNonQuery();
                                }
                                trans.Commit();
                            }
                            langUseUpdated = true;
                        }
                        SaveLanguageItems(this.BaseInstance, fallBackCulture);
                    }

                    if (loadItems)
                    {
                        List <string> localProps = LocalizedPropsMySQL(AppForm, CultureInfo.CurrentCulture);
                        GetGuiObjets(CultureInfo.CurrentCulture, localProps);
                        LoadLanguageItems(CultureInfo.CurrentCulture);
                    }

                    if (!loadItems)
                    {
                        SaveMessages(messageResource, Assembly.GetExecutingAssembly(), ref MySQLConnection);
                    }

                    ts = ts.Add((DateTime.Now - dt));
                }
                catch (Exception ex)
                {
                    if (ex.GetType() == typeof(MissingManifestResourceException)) // This is fun. The user actually gets some help from the exception message ;-)
                    {
                        throw new LangLibException(string.Format("Missing resource '{0}'.{1}Perhaps {2}.[Resource file name] would do,{3}without the .resx file extension.",
                                                                 messageResource, Environment.NewLine, GetAssemblyName(this.appType), Environment.NewLine));
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 4
0
        public int ExecuteSqlTran(List <CommandInfo> list, List <CommandInfo> oracleCmdSqlList)
        {
            MySQLConnection connection = this.DbConn;
            int             result;

            using (MySQLCommand cmd = new MySQLCommand())
            {
                cmd.Connection = connection;
                DbTransaction tx = connection.BeginTransaction();
                cmd.Transaction = tx;
                try
                {
                    foreach (CommandInfo myDE in list)
                    {
                        string           cmdText  = myDE.CommandText;
                        MySQLParameter[] cmdParms = myDE.Parameters;
                        MyDbConnection3.PrepareCommand(cmd, connection, tx, cmdText, cmdParms);
                        if (myDE.EffentNextType == EffentNextType.SolicitationEvent)
                        {
                            if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
                            {
                                tx.Rollback();
                                throw new Exception("违背要求" + myDE.CommandText + "必须符合select count(..的格式");
                            }
                            object obj = cmd.ExecuteScalar();
                            if (obj == null && obj == DBNull.Value)
                            {
                            }
                            bool isHave = Convert.ToInt32(obj) > 0;
                            if (isHave)
                            {
                                myDE.OnSolicitationEvent();
                            }
                        }
                        if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
                        {
                            if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
                            {
                                tx.Rollback();
                                throw new Exception("SQL:违背要求" + myDE.CommandText + "必须符合select count(..的格式");
                            }
                            object obj = cmd.ExecuteScalar();
                            if (obj == null && obj == DBNull.Value)
                            {
                            }
                            bool isHave = Convert.ToInt32(obj) > 0;
                            if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
                            {
                                tx.Rollback();
                                throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须大于0");
                            }
                            if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
                            {
                                tx.Rollback();
                                throw new Exception("SQL:违背要求" + myDE.CommandText + "返回值必须等于0");
                            }
                        }
                        else
                        {
                            int val = cmd.ExecuteNonQuery();
                            if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
                            {
                                tx.Rollback();
                                throw new Exception("SQL:违背要求" + myDE.CommandText + "必须有影响行");
                            }
                            cmd.Parameters.Clear();
                        }
                    }
                    tx.Commit();
                    result = 1;
                }
                catch (MySQLException e)
                {
                    tx.Rollback();
                    throw e;
                }
            }
            return(result);
        }