Esempio n. 1
0
        public static long InsertNewMemberToDB(User user, SqlDatabaseClient client = null)
        {
            bool clientIsNull = client == null;

            try
            {
                string sql = "INSERT INTO Member (name, role) VALUES(@name, @role);";
                if (clientIsNull)
                {
                    client = SqlDatabaseManager.GetClient();
                }
                else
                {
                    client.SetParameter("@id", 51);
                    sql = "INSERT INTO Member (id, name, role) VALUES(@id, @name, @role);";
                }


                client.SetParameter("@name", user.Name);
                client.SetParameter("@role", user.Role);


                int count = client.ExecuteNonQuery(sql);

                long lastInsertedId = 0;
                if (count > 0)
                {
                    lastInsertedId = client.LastInsertedId();
                }

                if (clientIsNull)
                {
                    client.Dispose();
                }

                return(lastInsertedId);
            }
            catch (DbException e)
            {
                if (!clientIsNull)
                {
                    throw;
                }

                client?.Dispose();

                if (e.Message.Contains("unique_member_name_ck"))
                {
                    return(-2);
                }
                else
                {
                    Logger.WriteLine(e.ToString(), Logger.LOG_LEVEL.WARN);
                }
            }

            return(0);
        }